Whizbang Practice Exam 2 missed Flashcards
What is the total number of integers that array nums can hold in this code fragment?
int nums[][] = new int[3][3];
nums[0] = new int[2];
Please select : A. 6 B. 8 C. 9 D. Not valid array statement
Option B is the correct answer.
Here we have created a two-dimensional array with three rows and each of row will have three columns, so nums array can hold total 9 values. But then we have changed the first row of the two-dimensional array to refer array which can hold only two values. So, now total elements allowed will be 8. Hence, option B is correct.
What will be the output of this program?
public class Whiz{ public static void main(String[] args) {
final int [ ]ints = new int[3]; int len = ints.length; ints[1]++; for(int i : ints) System.out.print(i); } }
Please select : A. 000 B. 100 C. 010 D. An Exception is thrown. E. Compilation fails due to an error at line 6.
Option C is the correct answer.
At line 4, we have defined an integer array with final, but it doesn’t mean that all values in the array are final. It means only the reference is final, so at line 6, there won’t be any error. All array elements are initialized to their default values when array initialized, hence all elements of the array will be 0, at line 6 second element will be increased by one. So, when using enhanced for loop, 010 will be printed. Hence, option C is correct.
public class Whiz{
public static void main(String[] args) { int [][]ints = new int[3][2]; ints[0] = new int[3]; ints[2] = {1,2,3}; System.out.print(ints[0].length + ints[2].length); } } A. 4 B. 5 C. 6 D. An ArrayIndexOutOfBoundsException is thrown E. Compilation fails
Option E is the correct answer.
This code fails to compile due to an error at line 7, because the array constants can only be used in initializers. So, we can’t use “{1,2,3}” at line 7.
What will be the output of this program?
import java.util.Arrays;
public class Whiz { public static void main(String[] args) { int [ ][ ] ints = new int[2][];
Arrays.sort(ints[1]);
System.out.print(Arrays.toString(ints[1])); } } Please select : A. [0, 0, 0] B. [null,null,null] C. null D. NullPointerException E. Compilation fails
Option D is the correct answer.
Methods in the Arrays class throw a NullPointerException, if the specified array reference is null. So, here we haven’t initialized the ints[1], so it is null. Hence, passing it to Arrays sort method throws a NullPointerException.
What will be the output of this program?
interface I{ void meth(); }
class A implements I{ void A(String s){ } public void meth(){ System.out.print("A"); } }
class C extends A implements I{ public void meth(){ System.out.print("C"); } }
class Whiz{
public static void main(String args[]){
A a = new A();
C c1 = (C)a;
c1.meth();
}
}
Option C is the correct answer.
Here, we try to cast a superclass reference to lower class reference, but superclass reference refers to the superclass object. So, casting will cause a ClassCastException.
What will be the output of this program?
public class Whiz{
static int x = 2; public static void main(String args[]){ if( x > 1){ x++; int x = 4; } System.out.println(x); final int x = 10; } }
Please select : A. 2 B. 3 C. 4 D. 10 E. Compilation fails
Option B is the correct answer.
In the given code, we have defined a static variable x with value 2 hence at line 5 if block will be executed, and increment the value of x by one, at line 7, defining new integer with the same name will shadow the class variable x, but the scope of that variable is limited to the if block. Hence, the value of the class variable will be printed which is 3. So, option B is correct.
The integer variable x at line 10 is declared after the printing statement, so it won’t affect any statement above the line 10, but if there was more code after line 10, then that x would shadow the class variable x.
What will be the output of this program?
public class Whiz{
static int x = 2; static int z; public static void main(String args[]){ System.out.println(x+z); }
static{ int x = 3; z = x; } }
Please select : A. 2 B. 4 C. 5 D. 6 E. Compilation fails
Option C is the correct answer.
At line 10, we have defined new integer with value 3, it shadows the class variable x. Hence, the value of z will be 3. So, 2+3 will print 5 as the output. Hence, option C is correct.
What will be the output of this program?
public class Whiz { public static void main(String args[]) {
final int array [] = {1,2,3}; switch(2) { case array[0] : {System.out.print("A");} case array[1] : System.out.print("B"); default : System.out.print("default"); break; case array[2] : System.out.print("C"); } } }
Please select : A. ABdefault B. default C. Bdefault D. C E. Compilation fails
Option E is the correct answer.
Option E is correct as the code fails to compile due to line case statements. When using a primitive or reference type variables as case constants, we should keep in mind that they should be compile-time constant. But in given code, we have used variables from the primitive array, even the array is final, the elements of the array are not final hence code fails to compile.
What will be the output of this program?
public class Whiz{ public static void main(String args[]){ int marks = 60;
if(marks >= 40) System.out.println("C"); else if(marks >= 60) System.out.println("B"); else if(marks >= 75) System.out.println("A"); else System.out.println("D"); } }
Please select : A. A B. B C. C D. D E. Compilation fails
Option C is the correct answer.
This code compiles successfully and prints C as the output so option C is correct. But if you have a look carefully you can see that logic of using if else here is incorrect since any value greater than 40 will end up printing C which it is not supposed to. To fix this we need to use check for greater values first.
What will be the output of this program?
public class Whiz{ public static void main(String args[]){ Integer i = 10; Double d = 10.0; int ii = 10; double dd = 10.0;
System.out.print(i.equals(d) + " "); System.out.print(ii == dd); } }
Please select : A. true true B. true false C. false false D. false true E. Compilation fails
Option D is the correct answer.
While using equals method of wrappers, it checks the wrapper type and the wrapper primitive value. Hence, line 8 will result in false since i and d are not the same types. Comparison of primitives using ‘==’ will check the actual value of the variable refer not the type. Hence, at line 9, true will be printed. Hence, option D is correct.
What will be the output of this program?
class Whiz { public static void main(String args[]) { new Whiz().iterator(new int [ ]{10,12,13}); } void iterator(int [ ]i) { for(int x=0; x < i.length; System.out.print(i[x] + " "))x++; } }
Please select : A. 10 12 13 B. 12 13 C. 10 12 D. 12 13 followed by an exception E. Compilation fails
Option D is the correct answer.
We have passed the anonymous array to the iterator method which uses a for loop to iterate through array elements and print them. In given for loop, we have used the printing statement in the update part and we have done the increment part inside the loop. So, in the first iteration, it will increase the value of x and then print the second element. But when it does two iterations, the value of x will become 3, so in the final iteration, trying to access element will index position 3, will result in an exception. Hence, option D is correct.
What will be the output of this program?
abstract class Animal{ void run(){ System.out.print("Animal run"); } abstract void sound(); }
class Dog extends Animal{
void sound(){ System.out.print("Bark"); }
public void run(){ System.out.print(" Dog runs"); } } public class Whiz{ public static void main(String [] args){ Animal dog = new Dog(); dog.sound(); dog.run(); } }
Please select :
A. Bark Dog runs
B. Bark Animal runs
C. Compilation fails due to an error at line 10
D. Compilation fails due to an error at line 21
E. Compilation fails due to multiple errors
Option A is the correct answer.
Here at line 20, we have created Dog instance and have used a superclass Animal reference, so we can invoke any method of superclass Animal even the actual object is Dog. When invoking the overridden method at run time, the overridden version of the actual object will be invoked, so sound and run method of Dog class will be invoked. So, option A is correct.
Which of the following can be inserted at line 10 to invoke the read method?
class Person{}
class Student extends Person{ public void read(){System.out.println("Reading");} }
public class Whiz{ public static void main(String [] args){ Person stu = new Student(); //insert here } }
Please select : A. stu.read(); B. (Student)stu.read(); C. (Student)(stu).read(); D. ((Student)stu).read(); E. None of the above.
Option D is the correct answer.
At line 9, we have created a Student instance with reference Person, so it is impossible to invoke the read method using that reference directly even the actual object is Student compiler tries to find the read method of reference type. Hence, the option A is incorrect. Options B and C will do the same thing since the “.” operator has higher precedence if we use them then it will try to see the return value of the read method and tries to cast it which is illegal.
Option D is correct since we have correctly cast the object to Student using correct order of parentheses.
What will be the output of this program?
interface I{ public default void print(){ System.out.print("I"); } static void method(){ System.out.print("Static"); } }
public class Whiz{ public static void main(String [] args){ I i = new I(){}; i.print(); I.method(); } }
Please select :
A. IStatic
B. An exception is thrown
C. Compilation fails due to an error at line 2
D. Compilation fails due to an error at line 15
E. Compilation fails due to multiple errors
Option A is the correct answer.
From java se 8, we can have default and static non-abstract methods in interfaces but there are some rules. Both static and default methods in interfaces are public. At line 15, we have initialized an instance of type I. So, using it we can invoke the default method, which prints “I”. Then we can invoke the static method using the interface name or using the object reference, here we have to use Interface name to invoke its static method, which will print “Static”. Hence, option A is correct.
Which of the following is a valid long literal?
Please select : A. 0x99ffCl B. 12 C. 12.8 D. 11.2l E. None of the above.
Option A is the correct answer.
Using L suffix will make literal long, as long as it is valid non-fraction number hence option D is incorrect. Also, we can use decimal, hexadecimal, octal or binary literals, so option A is correct.
Option B is a valid integer literal but it can be assigned to long since long can store integers.