Whizbang Practice Final Missed Flashcards
What will be the output of this program code?
public class Whiz {
public static void main(String[] args) { String [][]strs = new String[5][]; strs[1] = new String[2]; System.out.print(strs.length + strs[1].length); } }
Please select : A. 2 B. 4 C. 7 D. A NullPointerException is thrown E. Compilation fails
Option C is the correct answer.
When we are using a two-dimensional array, the length variable of that array will represent the number of rows in a two-dimensional array so here at line 7 length field of the strs array will be 5, then accessing the length of a one-dimensional array in index position 1 will return 2 so total will be 7. Hence, option C is correct.
public class Whiz { static Integer i; public static void main(String[] args) { try { System.out.print(i.toString()); }catch(RuntimeException ex) { throw ex; }catch(Exception e) { System.out.print("e"); }finally { System.out.print("fin"); } } }
Option D is the correct answer.
Here at line 5, a null pointer exception will be thrown and it will be caught from the first catch box since null pointer exception is a subtype of Runtime exception. But again in the catch box throwing new exception will result in an uncaught exception, since the finally will execute at the end result will be fin, followed by an exception. Hence, option D is correct.
What will be the output of this program code?
public class Whiz {
public static void main(String[] args) { int ints[] = get(-1); System.out.println(ints.length); } public static int[] get(int x) { return new int[x]; } }
Please select : A. 0 B. -1 C. NegativeArraySizeException D. IllegalArgumentException E. NullPointerException
Option C is the correct answer.
This code throws a NegativeArraySizeException at line 10 as when we are using negative array size to initialize an array NegativeArraySizeException is thrown. So, option C is correct.
Which of the following exception will be thrown by the JVM when we try to access a char of a string using the length of the string?
Please select : A. NullPointerException B. StringIndexOutOfBoundsException C. NumberFormatException D. ArrayIndexOutOfBoundsException E. There will be no exception
Option B is the correct answer.
Option B is correct since the StringIndexOutOfBoundsException is thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string. The NullPointerException is thrown by the JVM when there is a null reference where an object is required. So, option A is incorrect.
Which of the following can be inserted at line 2, to make this code compile?
public void exc() throws FileNotFoundException{ // INSERT CODE HERE }
Please select : A. throw new java.io.IOException(); B. throw new RuntimeException(); C. throw new Exception(); D. throw new IOException(); E. All of the above
Option B is the correct answer.
A method that declares an exception isn’t required to throw one, and runtime exceptions can be thrown in any method, making option B correct.
Options A, C, and D are incorrect because a broader exception is not allowed.
Which of the following is platform independent?
Please select : A. JRE B. JDK C. JVM D. byte code E. None of the above
Option D is the correct answer.
The Java bytecode works on all Java virtual machines is that a rather strict standard has been written for how Java virtual machines work. This means that no matter what physical platform you are using, the part where the Java bytecode interfaces with the JVM is guaranteed to work only one way. Since all the JVMs work exactly the same, the same code works exactly the same everywhere without recompiling. So, option D is correct.
Other options are incorrect since the component of java are not platform independent but they provide the capability for bytecode to be platform independent.
What will be the output of this program code?
public class Whiz {
public static void main(String[] args) { String[ ] sts = {“A”,”B”,”C”}; for (String i : sts) { continue; System.out.print(i); } } }
Your answer is incorrect.
Explanation:
Option E is the correct answer.
Here at line 8, we have used continue because of that for each iteration of for each loop, line 9 can’t be reached. Hence, the compiler complains that line 9 is unreachable. So, option E is correct.
Which of the following has higher operator precedence than instanceof operator?
Please select : A. == B. != C. new D. += E. There is no such operator called instanceof.
Option C is the correct answer.
According to java operator precedence, only the relational operator ‘new’ has higher precedence than the instance operator from given options.
What will be the output of this code fragment?
public class Whiz { public static void main(String[] args) { long in = 3; final byte b = 0;
switch(in) { case b : System.out.print(0); case b+1 : System.out.print(1);break; case b+3 : System.out.print(3); case b+2 : System.out.print(2);break; default :System.out.print("?"); } } }
Please select : A. 32 B. 32? C. 132 D. 01 E. Compilation fails
Option E is the correct answer.
Option E is correct as the code fails to compile due to line 6. The Switch doesn’t allow long, float, double, and boolean values. The Switch works with the byte, short, char, int, Byte, Short, Character, Integer, String and Enum types.
Reference : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
The correct answer is: Compilation fails
What will be the output of this program code?
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
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 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 code fragment?
public class Whiz { public static void main(String[] args) { Base bs = new Subclass(); bs.display(); } }
class Base { public static void display() { System.out.println("Base"); } }
class Subclass extends Base { public static void display() { System.out.println("Subclass"); } }
Option B is the correct answer.
We can declare static methods with the same signature in a subclass but it is not considered overriding as there won’t be any run-time polymorphism. If a derived class defines a static method with the same signature as a static method in base class, the method in the derived class hides the method in the base class. As per overriding rules, this should call to class Subclass’s static overridden method. Since a static method cannot be overridden, it calls Base’s display(). Hence, option B is correct.
Which of the following has correct method signature for overriding version of this method which is located in an interface?
abstract int calculate()throws IOException;
Please select : A. int calculate()throws Exception; B. void calculate()throws IOException; C. public int calculate(); D. public void calculate()throws IOException; E. Given method is invalid
Option C is the correct answer
It is given that method is located in an interface so it is implicitly public.
Option A is incorrect since we can’t use more restrictive access level when overriding methods, so using default is incorrect. And also, we can’t throw a boarder checked exception.
Option B is incorrect since we can’t use more restrictive access level when overriding methods so using default is incorrect
Option C is correct since there we have used public as the access level and it is legal to skip throwing an exception for overridden method.
When overriding we can’t change the return type except for covariant types, hence option D is incorrect.
class Animal { public void eat() throws Exception { System.out.print("Animal eats");} }
class Dog extends Animal { public void eat() { System.out.print("Dog eats"); }
public static void main(String [] args) { Animal a = new Dog(); Dog d = new Dog(); d.eat(); a.eat(); } }
Option E is the correct answer.
If a method is overridden but we use a polymorphic (super type) reference to refer to the subtype object with the overriding method, the compiler assumes we are calling the supertype version of the method. If the supertype version declares a checked exception but the overriding subtype method does not, the compiler still thinks you are calling a method that declares an exception.
So, the above code fails to compile due to line 12, since compiler sees calling the eat method of the superclass and fail to catch or declare the exception.
Which of the following is a correct variable declaration?
Please select : A. int stu count; B. double null; C. float 12f; D. int x_y; E. None of the above.
Your answer is incorrect.
Explanation:
Option D is the correct answer.
Option D is correct since we can use “_” in a variable name.
Option A is incorrect since we cannot use spaces in a variable name.
Option B is incorrect since we cannot use a keyword for a variable name, here ‘null’ is a keyword.
Option C is incorrect since we can’t start a variable name with a digit.
How many objects are eligible for GC when line 12 is reached?
class Exam { Exam(Integer in){ code = in;} String s = “OCAJP”; Integer code; }
public class Whiz { public static void main(String [] args) { Integer c = 808; Exam w =new Exam(c); w = null; System.out.println(c); } } Please select : A. 1 B. 2 C. 3 D. 4 E. Compilation fails
Option B is the correct answer.
At line 10, we have created an Exam instance by passing a wrapper variable c. At line 11, making w reference null will cause the Exam instance eligible for the GC and also the String reference too. But the Integer code refers to the same place where the variable c refers so it won’t be eligible for the GC. Hence, at line 12, only 2 objects are eligible for the GC.