Core Java (GATE - CS)

Covers Java fundamentals including inheritance, arrays, strings, loops, access modifiers, and variable scope for GATE Computer Science exam preparation.

15 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

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
Question 2 Multiple Choice (Single Answer)

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
Question 3 Multiple Choice (Single Answer)

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
Question 4 Multiple Choice (Single Answer)

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
Question 5 Multiple Choice (Single Answer)

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
Question 6 Multiple Choice (Single Answer)

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
Question 7 Multiple Choice (Single Answer)

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
Question 8 Multiple Choice (Single Answer)

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
Question 9 Multiple Choice (Single Answer)

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
Question 10 Multiple Choice (Single Answer)

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
Question 11 Multiple Choice (Single Answer)

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
Question 12 Multiple Choice (Single Answer)

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
Question 13 Multiple Choice (Single Answer)

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
Question 14 Multiple Choice (Single Answer)

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
Question 15 Multiple Choice (Single Answer)

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