Core Java (GATE - CS)
Covers Java fundamentals including inheritance, arrays, strings, loops, access modifiers, and variable scope for GATE Computer Science exam preparation.
Questions
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 ();
}
}
- Removing the private modifier from the two () method
- Adding the public modifier to the declaration of class X
- Changing the private modifier on the declaration of the one() method to protect
- Removing the Y () constructor
- None of these
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]);
- 34
- 35
- 24
- Run time error
- Compilation error
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);
}
- They Match They Really Match
- They really Match
- They Match
- Blank Output Screen
- Compilation fails
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);
- They matchThey really match
- They really match
- They match
- Nothing will be printed
- Compilation fails
What is the output of the given code?
public class MyFor3
{
public static void main(String[] args)
{
int [] xx = null; System.out.println(xx);
}
}
- Null
- Java.lang.NullPointerException
- Compilation fails
- 0
- None of these
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));
}
}
- Blank output screen
- StringIndexOutofBound exception
- ArrayIndexOutofBound exception
- ‘\0’ will be printed
- Compilation fails
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++;
}
}
}
}
- Zero times
- Once
- Twice
- Thrice
- Compilation fails
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]);
}
}
- 35
- 25
- 24
- Run time error
- Compilation error
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);
}
}
- 012
- 01
- 0123
- Run time error
- Compilation error
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);
}
}
- 8
- 9
- 11
- Compile time error
- Run time error
Which of the following options are correct?
interface Pet { }
class Dog implements Pet { }
public class Beagle extends Dog{ }
- Pet a = new dog();
- Beagle c = new dog();
- Pet e = new Beagle();
- Pet b = new Pet();
- Both 1 and 3
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);
}
}
- Found 3 at 2
- Found 3 at 3
- Found 3 at 4
- Compilation fails
- Run time error
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++;
}
}
}
}
- Zero
- Once
- Twice
- Thrice
- Compilation error
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]);
- 4 null
- Null 4
- An exception is thrown at run time.
- 4 an ArrayIndexOutofbound exception is thrown.
- Compilation fails
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;
}
}
- 6 5 6 4
- 6 5 5 4
- 6 5 6 6
- Run time error
- Compilation fails