Whizbang Practice Exam 1 Missed Flashcards
What is the output of public class Whiz { public static void main(String [ ] args) { long [ ][ ] l2d; long [ ] l1d = {1,2,3}; Object o = l1d; l2d = new long[3][3]; l2d[0][0] = (long[])o; } }
Fails at l2d[0][0] = (long[])o;
tries to assign a one-dimensional array into a long
What is the output of
import java.util.Arrays;
public class Program { public static void main(String [ ] args) { String[ ] strings = {"N","L","n","O","S"}; Arrays.sort(strings); for(String s : strings){ System.out.print(s); } } }
Outputs LNOSn
sort():
- public static void sort(Object[] a);
- sorts the specified array of objects into ascending order
- all elements in the array must implement the Comparable interface otherwise it throws ClassCastException.
- sorts numbers before letters
- sorts upper case letters before lower case
What will be the output of this program code?
import java.util.Arrays;
public class Program { public static void main(String [ ] args) {
int[ ] a1 = {2,-1,4,5,3}; int[ ] a2 = {2,-1,4,5,3}; System.out.print((a1 == a2) + " "); System.out.print(Arrays.equals(a1, a2) + " " ); System.out.print(Arrays.deepEquals(a1, a2) + " " ); } }
Compilation Fails
Arrays class has deepEquals method which takes two object arrays. public static boolean deepEquals(Object[] a1,Object[] a2)
It returns true if the two specified arrays are deeply equal to one another. Unlike the equals(Object[],Object[]) method, this method is appropriate for use with nested arrays of arbitrary depth.
Here at line 11, we have invoked the deepEquals method by passing two int arrays. So, it will result in a compile error. deepEquals method accepts the Object array references and notes the primitive array references. So, option E is correct.
What will be the output of this program?
import java.io.FileNotFoundException; import java.io.IOException;
public class Whiz { public static void main(String [ ] args) { try { throw method(); } catch(IOException e) { System.out.println("caught"); } }
public static IOException method() { try { return new IOException(); } catch(FileNotFoundException e) { return new FileNotFoundException(); } } }
Fails at catch(IOException e)
Unreachable code
Which of the following will import all static members of java.util.Arrays class?
Please select : A. import static java.util.Arrays; B. import java.util.Arrays; C. import static java.util.Arrays.*; D. static import java.util.Arrays; E. import java.util.Arrays.*;
C is the correct answer.
When using static imports, even though the feature is commonly called “static import” the syntax MUST be the import static followed by the fully qualified name of the static member you want to import, or a wildcard.
What will be the output of this program code?
public class Whiz { public static void main(int [ ] i) { System.out.print("main1"); } public static void main(String... c) { System.out.print("main2"); } public static void main(String c) { System.out.print("main3"); } }
Please select: A. main1 B. main2 C. main3 D. An Error is thrown at the runtime, stating that, Main method not found in class Whiz. E. Compilation fails.
Option B is the correct answer.
The signature of the main method must take one form of the following two forms;
public static void main(String[] args) or public static void main(String… args)
And it can also be a final.
JVM calls main method. When JVM calls main method, it passes a zero length String array if there are no command line arguments passed when running program. So, main method defined at line 5 to 7 will be called. The starting point of a program is the main method; it simply means that the program starts to execute statements which are located inside the main method. So, here “main2” will be printed. Therefore, option B is correct.
What will be the output of this program?
public class Whiz { static int x = 50; public final static void main(String [ ] a) { Integer [ ] a = new Integer[2]; a[1] = 10; for (Integer I:a) System.out.print(I); } }
Please select : A. null10 B. 10 C. A null pointer exception is thrown. D. Compilation fails. E. None of the above.
Option D is correct as the code fails to compile. In the main method, we have declared the argument as “String[] a” and inside the main method we have tried to create an Integer type array with the name “a”. This will cause a compile time error as we can’t declare two variables with the same name inside the same scope. Here, both method argument and the Integer array belong to the same local scope.
Which of the following will result in false?
Please select : A. true | (false ^ true); B. (true | false ^ true); C. true | false ^ true; D. (true | false) ^ true; E. None of the above.
Option D is the correct answer.
Here we have evaluated the same expression “true |false ^ true” by applying parentheses in different positions.
If we consider the default precedence, the expression “false ^ true” will be evaluated before evaluating “true | false” which will produce true as the output.
Option D is correct as parentheses change the default precedence. Here “true | false” will be evaluated before evaluating “^” so the output will be false since “true ^ true” results false.
Options A, B and C are incorrect as the default precedence has not changed there.
Which of the following is true?
public class Whiz { public static void main(String args [ ]) { int y = 5;
if (false && y++==11) System.out.print(y); else if(true || --y==4) System.out.print(y); else( y==5){} } }
Please select : A. The output will be 6. B. The output will be 4. C. The output will be 5. D. There is no output. E. Compilation fails.
The code fails due to else( y==5) because we can’t use a conditional clause with else. So, option E is correct.
What will be the output of this program code?
class Whiz { public static void main(String [ ] args) { int x = 0; String [ ] animal = new String[3]; do{animal[x] = "Cat"; x++;} while(false); do{animal[x] = "Dog";} while(x>animal[x++].length()); do{animal[x] = "Rat";} while(x>3); for(String s:animal){ System.out.print(s + " "); } } }
Please select :
A. Cat Dog Rat
B. Dog Rat
C. A run-time exception is thrown.
D. Compilation fails due to an error at line 7.
E. Compilation fails due to an error at line 8.
Option A is the correct answer.
When using “do-while”, the statements in “do” block will be executed at least one time. So, here all do-while loops add elements to the String array even the all while loop conditions are false. So, the output will contain all three Strings added by three do-while loops.
What will be the output of this program?
public class Whiz { public static void main(String [ ] args) {
for ( int j = 0, k = 5; j < k; k--) ; for ( int j = 0; j++ < 3;) ; for ( int i = 0; i < 5; i++, System.out.print(i + ".Hi ")) ; } }
Please select :
A. 1.Hi 2.Hi 3.Hi 4.Hi 5.Hi
B. 0.Hi 1.Hi 2.Hi 3.Hi 4.Hi 5.Hi
C. An exception is thrown at runtime.
D. Compilation fails due to an error at line 6.
E. Compilation fails due to multiple errors.
Option A is correct as the code produces output “1.Hi 2.Hi 3.Hi 4.Hi 5.Hi”
Option B is incorrect as the “1.Hi” will be printed because “i++” executes before print statement.
Option C is incorrect as skipping any part of for loop doesn’t cause any compile time error. In fact, we can skip all three part if necessary.
Option D is incorrect because there is no issue in that for loop, many mistakenly call the third expression of for loop as “increment expression”. But we can put any virtually arbitrary code statements that you want to happen with each iteration of the loop.
Which of the following will produce the output as 1 3 8 when inserted at line 7 ?
public class Whiz { public static void main(String args [ ] ) {
int arr[ ][ ] = {{1, 3, 5},{7,8}}; out:for(int [ ]a : arr){ for ( int i: a ) { // insert code here } } } }
Please select : A. if ( i == 7 ) continue; System.out.print(i + " "); if ( i == 3) break out; } B. if ( i == 7) continue out; System.out.print(i + " "); if ( i == 3) break; } C. if ( i == 7) continue; System.out.print(i + " "); } D. if ( i == 7) continue; System.out.print(i + " "); if ( i == 3) break; } E. None of the above.
Option D is correct as it will produce the expected output.
Option C is incorrect as it will produce the output 1 3 5 8.
Options A and B are incorrect as they will produce the output 1 3.
What will be the output of this program?
class A { A method() { return new A(); } }
class B extends A { B method() { return new B(); } }
class Whiz { public static void main(String [ ] args) { //codes } }
Please select :
A. Compilation succeeds.
B. Compilation fails due to an error at line 2.
C. Compilation fails due to an error at line 8.
D. Compilation fails due to multiple errors.
Option A is the correct answer.
Overriding Methods can change the return type only within the bounds of covariant returns. It simply means that the overriding method can return a subtype of the return type of the superclass method. Before java 1.5, this code fails as java didn’t allow covariant return types in those versions. So, this code compiles successfully. Hence, option A is correct.
Which of the following method will override this method correctly?
Object supply() { return null; }
Please select :
A. public String supply() throws NullPointerException { return null; }
B. int supply() { return 0; }
C. public Object supply()throws Exception { return null; }
D. private Object supply() { return null; }
E. protected Object supply (int x) { return null; }
Option A is correct as it is well within overriding rules.
Option B is incorrect as the return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.
Option D is incorrect as the access level cannot be more restrictive than the overridden method’s access level.
Option E is incorrect since the argument list should be exactly the same as that of the overridden method.
Option C is incorrect since the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method.
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(); } }
Please select :
A. A
B. C
C. Compilation fails due to an error at line 6.
D. Compilation fails due to multiple errors.
E. An exception will be thrown at run-time.
Option E is the correct answer.
Option E is correct as here we try to cast a superclass reference to lower class reference, but superclass reference refers to superclass object. So, casting will cause a ClassCastException.
Options A and B are incorrect as the code throws an exception before producing any output.
Options C and D are incorrect as the code compiles successfully.