0

Core Java (GATE - CS)

Description: Core Java
Number of Questions: 15
Created by:
Tags: Core Java Java Basics
Attempted 0/15 Correct 0 Score 0

What changes will make the above code compile?

class X { X () { } private void one () { }
}
public class Y extends X { Y () { } private void two () { one();
}
public static void main (String [] args) { new Y().two ();
}
}

  1. Removing the private modifier from the two () method

  2. Adding the public modifier to the declaration of class X

  3. Changing the private modifier on the declaration of the one() method to protect

  4. Removing the Y () constructor

  5. None of these


Correct Option: D
Explanation:

This change will also not allow the code to compile because there is no error in that constructor.

What is the output of the given program?

int [] array = {1, 2, 3, 4, 5};

System.arraycopy (array, 2, array, 1, 2);

System.out.print (array [1]);

System.out.print (array[4]);

  1. 34

  2. 35

  3. 24

  4. Run time error

  5. Compilation error


Correct Option: B
Explanation:

The two elements 3 and 4 (starting from position with index 2) are copied into position index 1 and 2 in the same array. After the array copy command, the array looks like: {1, 3, 4, 4, 5}.Then, element with index 1 is printed: 3. Then, element with index 4 is printed: 5.

What is the output of the given program?

StringBuilder sb = new StringBuilder ();

String h1 = HelloWorld;

sb.append(Hello).append (world);

if (h1 == sb.toString())

{

System.out.println(They match);

}

if (h1.equals(sb.toString()))

{

System.out.println(They really match);

}

  1. They Match They Really Match

  2. They really Match

  3. They Match

  4. Blank Output Screen

  5. Compilation fails


Correct Option: D
Explanation:

Strings cannot be compared with the usual <, <=, >, or >= operators, and the == and != operators don't compare the characters in the strings.  

What is the output of the given code?

String message1 = Wham bam!;

String message2 = new String(Wham bam!);

if (message1 == message2)

System.out.println(They match);

if (message1.equals(message2))

System.out.println(They really match);

  1. They matchThey really match

  2. They really match

  3. They match

  4. Nothing will be printed

  5. Compilation fails


Correct Option: B
Explanation:

The strings are not the same objects. So, the == comparison fails. As the values of the strings are the same, equals is true. The equals method compares values for equality. == compares references, not values. The use of == with object references is generally limited to the following: Comparing to see if a reference is null. 

What is the output of the given code?

public class MyFor3
{
public static void main(String[] args)
{
int [] xx = null; System.out.println(xx);
}
}

  1. Null

  2. Java.lang.NullPointerException

  3. Compilation fails

  4. 0

  5. None of these


Correct Option: A
Explanation:

This is correct because an array variable (here xx) can very well have null value. Null is the reserved constant used in Java to represent a void reference.

What is the output of the given program code?

public class ScopeTest
{
int z;
public static void main(String[] args)
{
String theString = "Hello World";
System.out.println(theString.charAt(11));
}
}

  1. Blank output screen

  2. StringIndexOutofBound exception

  3. ArrayIndexOutofBound exception

  4. ‘\0’ will be printed

  5. Compilation fails


Correct Option: B
Explanation:

There are only 11 characters in the string hello world. The code theString.charAt(11) retrieves the 12th character, which does not exist. A StringIndexOutOfBoundsException is thrown. Exception in thread main is java.lang. StringIndexOutOfBoundsException. So, this answer is correct.

How many times is 2 printed as part of output?

public class ScopeTest
{
int z;
public static void main(String[] args){ String[] table = {"aa", "bb", "cc"};
for (String ss: table)
{
int ii = 0;
while (ii < table.length)
{
System.out.println(ss + ", " + ii);
ii++;
}
}
}
}

  1. Zero times

  2. Once

  3. Twice

  4. Thrice

  5. Compilation fails


Correct Option: D
Explanation:

The above program produces output: aa, 0aa, 1aa, 2bb, 0bb, 1bb, 2cc, 0cc, 1cc, 2. So, 2 will be printed thrice. Hence, this answer is false.

What is the output of the given program?

public class ScopeTest

{

public static void main (String [] args)

{

int [] array = {1, 2, 3, 4, 5};

System.arraycopy (array, 2, array, 2);

System.out.print (array [1]);

System.out.print (array[4]);

}

}

  1. 35

  2. 25

  3. 24

  4. Run time error

  5. Compilation error


Correct Option: E
Explanation:

This answer is correct.

What is the output of the given program code?

public class ScopeTest

{

public static void main (String [] args)

{

String[] table = {aa, bb, cc};

int ii =0;

do while (ii < table.length);

System.out.println(ii++);

while (ii < table.length);

}

}

  1. 012

  2. 01

  3. 0123

  4. Run time error

  5. Compilation error


Correct Option: E
Explanation:

This is correct. There is an error in the program.

What is the output of the given Java program?

public class MyClass
{
public static void main(String[] args)
{
String s = " Java Duke ";
int len = s.trim().length();
System.out.print(len);
}
}

  1. 8

  2. 9

  3. 11

  4. Compile time error

  5. Run time error


Correct Option: B
Explanation:

This is the correct choice. The program will print 9 as output. 

Which of the following options are correct? interface Pet { } class Dog implements Pet { } public class Beagle extends Dog{ }

  1. Pet a = new dog();

  2. Beagle c = new dog();

  3. Pet e = new Beagle();

  4. Pet b = new Pet();

  5. Both 1 and 3


Correct Option: E
Explanation:

Both options 1 and 3 are correct. So, this is the correct choice.

What is the output of the given program?

public class arr
{
public static void main(String[] args)
{
int array[] = {0, 1, 2, 3, 4};
int key = 3;
for (int pos = 0;
pos < array.length; ++pos)
{
if (array[pos] == key)
{
break;
}
}
System.out.print("Found " + key + "at " + pos);
}
}

  1. Found 3 at 2

  2. Found 3 at 3

  3. Found 3 at 4

  4. Compilation fails

  5. Run time error


Correct Option: D
Explanation:

The following line does not compile: System.out.print (found + key + at + pos). 

How many times is 0 printed as part of output?

public class ScopeTest
{
int z;
public static void main(String[] args)
{
String[] table = {aa, bb, cc};
for (String ss: table)
{
int ii = 0;
while (ii < table.length)
{
System.out.println(ss + , + ii);
ii++;
}
}
}
}

  1. Zero

  2. Once

  3. Twice

  4. Thrice

  5. Compilation error


Correct Option: D
Explanation:

This is the correct choice. The for statement, for (String ss: table) is executed one time for each of the three elements in table. 

What is the output of the given code?

Int [] [] array = {{0}, {0, 1}, {0, 2, 4}, {0, 3, 6, 9}, {0, 4, 8, 12, 16}};
Systemout.printIn(array [4] [1]);
System.out.printIn (array) [1][4]);

  1. 4 null

  2. Null 4

  3. An exception is thrown at run time.

  4. 4 an ArrayIndexOutofbound exception is thrown.

  5. Compilation fails


Correct Option: D
Explanation:

This is the correct answer. The first print in statement, system.out.println(array [4][1]), works fine. It selects the element/array with index 4, {0, 4, 8, 12, 16} and from this array, it selects the element with index 1, which is 4. So, 4 gets printed. The second print in statement, system.out.println(array) [1][4]) fails. It selects the array/element with index 1,{0, 1} and from this array, it tries to select the element with index 4. This causes an exception. So, an exception in thread main java.lang.ArrayIndexOutOfBoundsException occurs.

What is the output of the given program?

public class ScopeTest
{
int z;
public static void main(String[] args)
{
ScopeTest myScope = new ScopeTest();
int z = 6;
System.out.println(z);
myScope.doStuff();
System.out.println(z);
System.out.println(myScope.z);
}
void doStuff()
{
int z = 5;
doStuff2();
System.out.println(z);
}
void doStuff2()
{
z = 4;
}
}

  1. 6 5 6 4

  2. 6 5 5 4

  3. 6 5 6 6

  4. Run time error

  5. Compilation fails


Correct Option: A
Explanation:

Within main program, z is assigned value 6. So, z = 6 is printed first. Within doStuff, z is assigned 5. DoStuff2 locally sets z to 4 (but MyScope.z is set to 4), but in dostuff, z is still 5. So, z=5 is printed. Again z is printed within main (with local z set to 6). So, z = 6 is again printed and finally myScope.z is printed. 

- Hide questions