0

Java

Description: Java, programming
Number of Questions: 15
Created by:
Tags: Java Concept Base Questions Java Basics
Attempted 0/15 Correct 0 Score 0

What is the output of the given program?

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

  1. Compilation Fails

  2. Null

  3. 0

  4. Java.lang.ArrayIndexOutOfBoundException

  5. Java.lang.NullPointerException


Correct Option: B
Explanation:

This is the correct choice because an array variable (here xx) can very well have the null value. Null is the reserved constant used in Java to represent a void reference, i.e. a pointer to nothing. Internally, it is just a binary 0, but in the high level Java language, it is a magic constant, quite distinct from zero, that internally could have any representation. So, this answer is correct.

Which of the following packages are automatically imported into the java source file by the java compiler?

Public Class Test
{
}

  1. Java.lang

  2. Java.awt

  3. The package with no name

  4. Java.*

  5. Both (1) and (3)


Correct Option: E
Explanation:

Both option (1) and option (3) are correct, so this is the correct choice.

What is the output of the given program?

public class ScopeTest
{
public static void main (String [] args)
{
int ii = 2;
do
{
System.out.println (ii);
}
while (--ii);
}
}

  1. 1

  2. 2

  3. Null

  4. Infinite loop

  5. Compilation Error


Correct Option: E
Explanation:

This is the correct choice because the line while (--ii); will cause the compilation to fail. --ii is not a boolean value. A correct line would be while (--ii>0); we can pass only Boolean values to the while loop. So, this is the correct choice.

Which of the following is the output of the given program?

public static void main(Stirng[] args)
{
int x = 353;
int j = x++;
switch (j)
{
case 317: case 353: case 367: System.out.println(Is a prime number.);
case 353: case 363: System.out.println(Is a palindrome.);
break;
default: System.out.println(Invalid value.);
}
}

  1. It is a prime number.

  2. It is a palindrome.

  3. It is a prime number. It is a palindrome.

  4. Invalid value

  5. Compilation fails


Correct Option: E
Explanation:

This is the correct choice. The program contains a compilation error. The switch contains two duplicate case values (353) which is not allowed in java. 

What is the output of the given program code?

public class ScopeTest
{
int z;
public static void main(String[] args)
{
ScopeTest myScope = new ScopeTest();
int z = 6;
System.out.println(myScope.z);
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. Garbage value 6 5 6 4

  2. 6 6 5 6 4

  3. 0 6 5 6 4

  4. Compilation error

  5. Run time error


Correct Option: C
Explanation:

This is the correct choice. Firstly, myscope.z will be printed, but it has not been initialised to any value. Initially it has a default value 0. So, 0 gets printed. Within main program, z is assigned the 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). Then z = 6 is again printed and finally MyScope.z is printed. MyScope.z has been set to 4 within doStuff2(). So, z = 4 gets printed. So, the correct output is 6 5 6 4. This is the correct choice.

What is the output of the given program?

public class ScopeTest
{
public static void main (String [] args)
{
int[][] xx[][][] = null;
if(xx)
{
System.out.println(xx);
}
else
{
System.out.println("False");
}
}
}

  1. Null

  2. False

  3. Java.Lang.NullPointerException

  4. 0

  5. Compilation fails


Correct Option: E
Explanation:

This is the correct choice. Line if(xx) contains an error because we can pass only Boolean value to the if() condition, but here null is passed. So, the compiler will raise an error that “cannot be converted to Boolean”. So, this answer is true.

Which of the given classes has default constructor? Class X { }

Class Y { Y() {} }

Class Z { Z(int i) { } }

  1. X Only

  2. Y Only

  3. Z Only

  4. X and Z

  5. X and Y


Correct Option: A
Explanation:

This is the correct choice as if a class doesn’t explicitly declare the default constructor, java compiler automatically creates a non-argument constructor for that class. So, a default constructor for class X will be created at run time automatically. So, this answer is true.

Which of the following are valid declarations for two dimensional array?

  1. Int[][] Array;

  2. Int[] Array[];

  3. Int[2][2] Array;

  4. Both (1) and (2)

  5. All of these


Correct Option: D
Explanation:

Both options (1) and (2) are valid for declaring the two dimensional array. So, this answer is correct.

What is the output of the given program code?

public class ScopeTest
{
public static void main(String[] args)
{
int [] xx = null;
for(int i=0;i

  1. Null

  2. Java.lang.NullPointerException

  3. 0

  4. Compilation fails

  5. None of these


Correct Option: B
Explanation:

This is the correct choice. Java.lang.NullPointerException will be thrown as we are trying to access the array that has been assigned a null value. So, exception will be thrown at run time. So, this is the correct choice.

How many times is “aa” printed as a 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:

This is the correct choice. The for statement, for (String ss: table), is executed one time for each of the three elements in table. The output comes out to be : aa, 0aa, 1aa, 2bb, 0bb, 1bb, 2cc, 0cc, 1cc, 2. So, “aa” will be printed 3 times, hence this answer is correct.

What is the output of the given program?

class ScopeTest
{
int x(double d)
{
System.out.println("one");
return 0;
}
String x(double d)
{
System.out.println("two");
return null;
}
double x(double d)
{
System.out.println("three");
return 0.0;
}
public static void main(String[] args)
{
new Overloading().x(4.0);
}
}

  1. One

  2. Two

  3. Three

  4. Compilation fails

  5. Run time error


Correct Option: D
Explanation:

This is the correct choice. Overloading of the x method fails as the input argument in all three cases are double. To use overloading of methods, the argument types must be different. Note: The Java programming language supports overloading methods and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists.

What is the output of the given program code?

public class ScopeTest
{
public static void main(String[] args)
{
ScopeTest sc, scA, scB;
sc = new ScopeTest();
scA = new ScopeTestA();
scB = new ScopeTestB();
System.out.println("Hash is : " + sc.getHash() + ", " + scA.getHash() + ", " + scB.getHash());
}
public int getHash()
{
return 111111;
}
}
class ScopeTestA extends ScopeTest
{
public long getHash()
{
return 44444444;
}
}
class ScopeTestB extends ScopeTest
{
public long getHash()
{
return 999999999;
}
}

  1. Hash is: 111111, 44444444, 999999999

  2. Blank output screen

  3. An exception is thrown at runtime

  4. Compilation Fails

  5. None of these


Correct Option: D
Explanation:

This is the correct choice. The compilation fails as SampleClassA and SampleClassB cannot override SampleClass’s getHash() method because the return type of SampleClass’s getHash() method is int, while the return type of SampleClassA and SampleClassB is long. Note: If all three classes had the same return type, the output would be: Hash is : 111111, 44444444, 999999999 So, this is the correct choice.

What is the output of the given program?

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

  1. 1 null

  2. Null null

  3. 2 an ArrayIndexOutofbound Exception is thrown

  4. ArrayIndexOutofbound Exception is thrown

  5. Compilation fails


Correct Option: C
Explanation:

This is the correct answer. The first println statement, System.out.println(array [2][1]), works fine. It selects the element/array with index 2, {0, 2,4}, and from this array it selects the element with index 1 which is 2. So, 2 gets printed. The second println statement, System.out.println(array) [2][4]), fails. It selects the array/element with index 2,{0, 2, 4}, 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?

class A
{
int a;
A(int a)
{
System.out.println(a);
}
public void display()
{
System.out.println("Hello");
}
}
class ScopeTest extends A
{
public static void main(String[] args)
{
ScopeTest s1 = new ScopeTest();
s1.display();
}
}

  1. Hello

  2. 0

  3. Run time error

  4. Compilation error

  5. Blank output screen


Correct Option: D
Explanation:

This is the correct choice. Class ScopeTest extends class A. When we create an object of class ScopeTest, it automatically calls the non-argument constructor of class A, but there is no such constructor in class A. So, the compiler will throw an error. If we add a non-argument constructor (as shown below) in class A, then the code will compile.A(){} So, this answer is true.

How many times is 1 printed as a 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:

This is the correct choice. For statement, (String ss: table), is executed one time for each of the three elements in table. The while loop will print 1 once for each element. The output comes out to be : aa, 0aa, 1aa, 2bb, 0bb, 1bb, 2cc, 0cc, 1cc, 2 So, 1 will be printed 3 times, hence this answer is correct.

- Hide questions