Whizbang Practice Exam 3 Missed Flashcards
What will be the output of this program code?
class Whiz { public static void main(String[] args) { int a[ ][ ] = new int[3][]; a[1] = new int[]{1,2,3}; a[2] = new int[]{4,5}; System.out.print(a[1][2]); } }
Please select :
A. 2
B. 3
C. Exception will be thrown at runtime.
D. Compilation fails due to an error at line 3.
E. Compilation fails due to an error at line 4.
Option B is the correct answer.
At line 3, we created a two-dimensional array with the first dimension as 3 so there can be three rows, then at lines 4 and 5, we assigned two anonymous one-dimensional arrays to last two rows of the two-dimensional array. At line 6, we tried to print the second-row third element which is 3. Note here we haven’t given any value to the first row of the two-dimensional array. So, option B is correct.
Reference : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
The correct answer is: 3
What will be the output of this program code?
- import java.util.Arrays;
- public class Whiz{
- public static void main(String[] args) {
- int[ ] ints = {3,6,1,4,0};
- Arrays.sort(ints,0,4);
- for(int i : ints) {
- System.out.print(i);
- }
- }
- }
Please select : A. 01436 B. 01346 C. 13460 D. An Exception is thrown. E. Compilation fails.
Option C is the correct answer.
One version of the sort method of Arrays class sorts the specified range of the array into ascending order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive.
public static void sort(int[] a,int fromIndex,int toIndex)
Here we have passed range 0 to 4, which means elements from first element (inclusive) to 4th element. So only they will be sorted i.e. 3,6,1,4 will be sorted and the final output is 13460. Hence, option C is correct.
Which of the following error/exception is typically thrown by JVM?
Please select : A. IllegalStateException B. AssertionError C. StackOverflowError D. ArrayIndexOutOfBoundsException E. All of the above.
Option C and D are the correct answer.
StackOverflowError, ArrayIndexOutOfBoundsException will be thrown by JVM. Hence Options C, D are correct. Options A, B are incorrect because IllegalStateException, AssertionError can be thrown programmatically.
Which of the following is checked exception?
Please select : A. ClassCastException B. NullPointerException C. ExceptionInInitializerError D. IllegalArgumentException E. None of the above.
Option E is the correct answer.
There are three kinds of exceptions in Java.
- checked exception
- error
- runtime exception
- Checked exception:
These are exceptional conditions that a well-written application should anticipate and recover from.
All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses. - Error:
These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. All direct or indirect subclasses of Error are errors. Ex : ExceptionInInitializerError etc - Runtime exception:
These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from.All direct or indirect subclasses of RuntimeException are the runtime exceptions.
Ex : ClassCastException, NullPointerException, IllegalArgumentException, etc.
Errors and runtime exceptions are collectively known as unchecked exceptions.
Which will compile successfully when inserted at line 3?
abstract interface Movable{ int x = 10; //insert code here void run(); }
Please select : A. private int x = 10; B. abstract int i = 5; C. final static float c = 6.0; D. final short s=10; E. None of the above.
Option D is the correct answer.
Option A is incorrect since at line 2 we already defined a variable calls ‘x’ so we can’t have another variable with the same name in the same scope. Also, the private variables are not allowed in interfaces.
Option B is incorrect as an abstract modifier is not valid for a variable.
Option C is incorrect since float literal should be ended with ‘f’ when it has decimal points.
Option D is correct as we can assign int literal within the range of -32768 – 32767 to a short.
Which of the following uses a fully qualified name in java?
Please select : A. class MyDate extends java.util.Date{ //statement; } B. import java.util.*; class MyDate extends Date{ //statement; } C. import java.util.Date.*; class MyDate extends Date{ //statement; } D. None of the above.
Option A is the correct answer
A fully qualified means using the complete package details when accessing a Java class. For example, if you are using java.lang.String is a fully qualified name for the class String. Here “java.lang” is the package where String class is declared and implemented. Other options are using the wild card “*” to import the classes and not using the fully qualified name to access the classes.
So, in this case, option A uses fully qualified name for the date class.
What will be the output of this program code?
import java.util.*;
class Whiz { public static void main(String args[]){ System.out.println(new Date()); } }
Please select : A. Prints today date B. An Exception C. Compile Error at line 1 D. Compile Error at line 5.
Option A is the correct answer
A fully qualified means using the complete package details when accessing a Java class. For example, if you are using java.lang.String is a fully qualified name for the class String. Here “java.lang” is the package where String class is declared and implemented. Other options are using the wild card “*” to import the classes and not using the fully qualified name to access the classes.
So, in this case, option A uses fully qualified name for the date class.
What will be the output of this program code?
import java.util.*;
class Whiz { public static void main(String args[]){ System.out.println(new Date()); } }
Please select : A. Prints today date B. An Exception C. Compile Error at line 1 D. Compile Error at line 5.
Option A is the correct answer.
Date class is present in Java since JDK 1.0. It is present in java.util package. It will contain current date, time, day name, time, and zone information. So, at line 5, statement prints current date, time, day name, time, and zone information.
Which of the following import statement will be inserted at line 1?
// insert here
public class Whiz { public static void main(String args[ ]) { System.out.println(pow(5,5)); } }
Please select : A. import java.lang.Math.*; B. import static java.Math.*; C. import static java.lang.Math.pow; D. import java.Math.pow; E. No import statement is needed.
Option C is the correct answer.
In given code, we have used the static pow method of Math class directly, so we have to import statically that method or all static members of the class. Correct syntax to import pow method statically is;
import static java.lang.Math.pow;
So, option C is correct.
What will be the output of this program?
class Whiz {
static int i; int j; Whiz() { j=i++; }
public static void main(String args[]) { Whiz s = new Whiz(); Whiz s1= new Whiz(); Whiz s2= new Whiz(); System.out.print( "i = "+s.i); System.out.print( ", j = "+s.j); } } What is the result?
Please select : A. i = 3, j = 3 B. i = 1, j = 0 C. i = 3, j = 0 D. An Exception is thrown. E. Compilation fails.
Option C is the correct answer.
In the above snippet, the variable i is static and j is in non-static variables. A static variable is associated with the class has only one copy per class but not for each object, Whereas a Non-static variable will have one copy each per object. Each instance of a class will have one copy of non-static variables. Since the variable i is common for every instance it will get increment each time instance is created so the values of i is 3 and j is 0. (While creating an instance, i is incremented and retains its value)
What will be the output of this program?
class Whiz{
final int i;
public static void main(String args[]){ Whiz s= new Whiz(); System.out.println( "i = "+s.i); } }
Please select : A. Prints i = 0 B. Prints i = 1 C. Prints i = Null D. Runtime Exception is thrown. E. Compile-time Error.
Option E is the correct answer.
The compiler complains “variable i has not been initialized in the default constructor”. A final variable is equivalent to a constant entity in java. Once it is initialized it may not be changed. But, it has to be initialized at the time of declaration. It can be initialized in 3 ways.
A final variable is initialized at the time of declaration
final int i=100;
It can be initialized in the constructor
Whiz(){i=100;}
It can be initialized in instance block of initialization.
What will be the output of this program?
public class Whiz { public static void main(String [ ] args) { do { int i = 3; System.out.print(i++); } while (i <= 6); } }
Please select : A. 333 B. 3456 C. 333333 D. Infinite loop of 3 E. Compilation fails
Option E is the correct answer.
The variable i is declared within the body of the do-while statement, so it is out of scope on line 6. Line 6 generates a compiler error, so option E is correct.
What will be the output of this program?
public class Whiz {
public static void main(String [ ] args) { String out = ""; z: for (int i = 3; i< 8; i++) { if (i == 3) continue; if (i== 3) break z; out += i; } System. out .println(out); } }
Please select : A. 4 B. 4567 C. 34567 D. No output will be produced. E. Compilation fails due to multiple errors.
Option B is the correct answer.
For loop at line 5 iterates till the value of i reaches 8, so the value of i will be increased 1 by 1 from 3 to 8. When the value of the variable i is 3, first “if” test will execute and skip concatenating the value of i to the String out. Since the first if statement, second if statement does not execute so loop won’t break there. So, the String out will consist of concatenated values from 4 to 7 i.e. “4567”. Hence, option B is correct.
Which of the following will compile and produce BC when inserted at line 4?
class Whiz { public static void main(String [ ] args) { final int s = 2; // insert code here switch(s) { case 1 : System.out.print("A");break; case x-1 :System.out.print("B"); case x : System.out.print("C");break; case x+1 : System.out.print("D");break; default : System.out.print("F"); } } }
Please select : A. int x=2; B. final int x=2; C. int x=3; D. private final int x = 3; E. None of the above.
Option E is the correct answer.
The case constant has to be a compile-time constant, so we can’t use option A and C. Option D is incorrect since we can’t use private access modifier inside the local scope.
Option B is incorrect since it will produce two cases with same value 1 which is illegal. Hence, option E is correct.
What will be the output of this program?
class Whiz { public static void main(String[ ] args) { int i = 3, j = 2; System.out.println(i-- + --j + ++i); } }
Please select : A. 8 B. 7 C. 6 D. 5 E. Compilation fails.
Your answer is incorrect.
Explanation:
Option B is the correct answer.
The increment/decrement operators can be applied before or after the operand. At Line 4 i– and –j will end in i and j being decremented by one respectively. Now, i is 2 and j is 1. ++i will increment the value of i by one. Now, i is 3 and j is 1. So, the statement at line 4 becomes as follows
System.out.println(3 + 1 + 3);
Hence output will be 7, option B is correct.