Whizbang Practice Exam 1 Missed Flashcards

1
Q
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;
                    }
       }
A

Fails at l2d[0][0] = (long[])o;

tries to assign a one-dimensional array into a long

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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);
                                       }                             
       }
   }
A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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) + " " );
     }    }
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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();
                        }
               }
    }
A

Fails at catch(IOException e)

Unreachable code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.*;
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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.
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.
A

The code fails due to else( y==5) because we can’t use a conditional clause with else. So, option E is correct.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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; }

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which of the following statement is true?

Please select :
A. An abstract class must use the abstract keyword when declared
B. An abstract class must have one or more abstract methods
C. An abstract class cannot extend a non-abstract class
D. An abstract class can be final.
E. An abstract class doesn't have a constructor
A

Option A is the correct answer.

An abstract class can contain zero or more abstract methods and can extend other classes, whether they are abstract or not. It is common to find abstract classes that implement an interface. Like any other class, abstract classes have constructors.

Abstract classes are meant to be extended, so it is illegal to mark an abstract class as final.

We must use the abstract keyword to declare an abstract class. So, option A is correct.

17
Q

Which of the following statement(s) is/are true?

I. The variables declared inside a method are called member variables.

II. The instance variables are initialized to their default values.

III. The variable “x” in the declaration “int x = 10” should be an instance variable.

Please select :
A. Only I.
B. Only II.
C. Only III. 
D. Only I and II.
E. Only I and III.
A

Option B is the correct answer.

Option B is correct as only the statement II is correct.

Statement II is correct as the instance variables and class variables are initialized to its default value.

Statement I is incorrect since the variables inside a method are called as local variables.

Statement III is incorrect as we can’t say anything about the variable “x” as it could be a local variable or instance variable because it doesn’t use any modifier. If It has used a modifier except “final” then it should be an instance variable.

18
Q

What will be the output of this program?

    class Whiz implements A {
                    public static void main(String args [ ]) {
                             System.out.print(A.s);
                             System.out.print(A.value);
                    }
    }
    interface A {
                    static int value = 15;
                    String s = "Value is: ";
  }

Please select :
A. Value is: 15
B. Compilation fails due to an error at line 3.
C. Compilation fails due to an error at line 4.
D. Compilation fails due to multiple errors.
E. An exception will be thrown at runtime.

A

Option A is the correct answer.

The interface variables are implicitly static so we can access them using interface name and also we can access them directly from a static context. So, this code will compile fine and produce the output as “Value is:15”. Therefore, option A is correct.

Option B is incorrect because we can access interface variable with interface name or with reference variable. We can also access interface variable without interface name or reference variable.

Option C is incorrect as we can also access interface variable using interface name.

Option D is incorrect since there are no compile time errors.

19
Q

Choose the option that contains only primitive literals.

Please select :
A. 1, ‘c’, “a”
B. 1, 1.5f, True 
C. ‘BF’, 10, “Sure”
D. 1.2D, 1f, ‘c’
E. None of the above.
A

Option D is correct since all are primitive literals, they are double, float and char.

Option A is incorrect as “a” is a String literal.

Option B is incorrect as True is incorrect literal; it should be true.

Option C is incorrect as ‘BF’ is illegal; char literal can only have one letter.

20
Q

What will be the output of this program?

public class Whiz {

                int x = y;
                static int y = 10;
                    public static void main(String args [ ] ) {
                                    System.out.print(new Whiz().x + " ");
                                    System.out.print(y);
                    }
              static{   y = 15;   }   }

Please select :
A. 10 15
B. 10 10
C. 15 15
D. Compilation fails due to an error at line 3.
E. Compilation fails due to multiple errors.

A

Option C is the correct answer.

When a class is loaded, the static fields are initialized and also the static blocks are executed. So, when the class Whiz is loaded, y is initialized to 10 but then the static block at line 11 changes the value of y to 15. So, the last value of the variable y is 15. Not like static variables, the instance variables are initialized when objects are created. So, here the value of x is also 15. Therefore, the output is 15 15. So, option C is correct.

Options A and B are incorrect as explained above.

Option D is incorrect as there is no issue at line 3 because the variable y is initialized before x get initialized.

21
Q

Which of the following statement is valid?

Please select :
A. Long l = 3;
B. Double d = 10;
C. Float f = 1.3; 
D. Float f = 3;
E. None of the above.
A

Option E is the correct answer.

Literals in int range are considered as int implicitly. So, in options A, B and D we have int primitive type literals. So, all of them are incorrect since widening followed by auto-boxing is not allowed in Java.

Option C is incorrect, there we have double value because we haven’t use f at the end to make a literal float.

22
Q

Which class of the following will compile and use a default constructor?

Please select :
A. public class Pen { void Pen() { } }
B. public class Pen { private Pen() { } }
C. public class Pen { public Pen(String name) { } }
D. public class Pen { public pen() { } }
E. public class Pen { abstract void Pen(); }
A

Option A is the correct answer.

Option A defines a method with name Pen but not a constructor. Since no constructor is coded, a default constructor is provided for option A. Hence, the option A is correct.

Option D doesn’t compile because the constructor name must match the class name. Since Java is case sensitive, it doesn’t match.

Options B and C compile and provide one user-defined constructor. Since a constructor is coded, a default constructor isn’t supplied.

Option E is incorrect since class should be abstract, so it doesn’t compile.

23
Q

Which of the following statement(s) is\are true about this method signature?

public void main(String s)

Please select :
A. Access level of this method is more restrictive than protected access level.
B. This is the main method.
C. It returns a String.
D. It is an invalid method signature. 
E. None of the above.
A

Option E is the correct answer.

The given method signature has the following characteristics -

  • It returns nothing since it has “void” as the return type.
  • It takes a String as an argument.
  • It has the public access level.

Option A is incorrect since public access level is less restrictive than protected access level.

Option B is incorrect as even this method has “main”. As its name, it is not the main method because the main method signature is unique.

Option C is incorrect as the method returns nothing.

Option D is incorrect since this method signature is completely legal.

24
Q

What is the order of execution of Initialization blocks and constructor in Java?

A

1) Static initialization blocks will run whenever the class is loaded first time in JVM
2) Initialization blocks run in the same order in which they appear in the program.
3) Instance Initialization blocks are executed whenever the class is initialized and before constructors are invoked. They are typically placed above the constructors within braces.
4) Constructors

25
Q
What is the output of
// Java code to illustrate order of
// execution of constructors, static
// and initialization blocks
class GFG {
GFG(int x)
{
    System.out.println("ONE argument constructor");
}

GFG()
{
    System.out.println("No  argument constructor");
}

static
{
    System.out.println("1st static init");
}

{
    System.out.println("1st instance init");
}

{
    System.out.println("2nd instance init");
}

static
{
    System.out.println("2nd static init");
}
    public static void main(String[] args)
    {
        new GFG();
	System.out.println("");
        new GFG(8);
    }
}
A
1st static init
2nd static init
1st instance init
2nd instance init
No  argument constructor

1st instance init
2nd instance init
ONE argument constructor

26
Q

Which of the following statement is true about this program code?

I. This code hasn’t correctly implemented the encapsulation principals.

II. Ths “getI” method at line7 has correctly implemented the encapsulation principals but not the variables.

III. Variables in this code have correctly implemented the encapsulation principals but not the “getI” method.

        class Whiz {
        public int i;
        public char c;
                    public static void main(String [ ] args) {
                                    //codes
                    }
                    private int getI() {
                                    return i;
                    }
      }         
Please select :
A. I only
B. II only 
C. I and II only
D. I and III only
E. None of the statement is true.
A

Option A is the correct answer.

Statement I is correct as this code completely disagrees with encapsulation principals.

Statement II and III are incorrect as both methods and variables in this class doesn’t implement the encapsulation principals. Here all variables are declared as public which allows anyone to modify variables without using methods and also methods have declared to be private which should have declared as public.

Option A is correct since only the statement I is correct.

27
Q

What is the result of the following code ?

public class Test {
          public static void main(String[] args) {
                    boolean name = true;
                    switch (name) {
                             case true:
                                       System.out.println("Hello ");
                             default:
                                        System.out.println("hi");
                              }
              }
}
Please select :
A. Hello 
B. Hi
C. Hello hi
 D. Compilation error.
 E. None of the above.
A

Option D is correct because switch statement doesn’t allow boolean data type. A switch allows byte, short, char, and int primitive data types and their corresponding wrapper classes Character, Byte, Short, Integer. It also allows String class and Enum types.

28
Q

What will be the output of this program?

class Whiz {
                private String code = "1Z0-808";
            { System.out.print(code+ " "); }

            private static int QUESTIONS = 90;

            static { System.out.print(QUESTIONS + " "); }

            static { QUESTIONS -= 13; System.out.print(QUESTIONS+ " "); }
                public Whiz() {
                                System.out.println("constructor " );
                }
}
public class Program {
                public static void main(String args[ ]) {
                                                Whiz wh = new Whiz();
                }
}
Please select :
A. 90 77 1Z0-808 constructor 
B. 1Z0-808 90 77  constructor 
C. 90 1Z0-808 77 constructor
D. constructor 90 1Z0-808 77 
E. Compilation fails.
A

Option A is the correct answer.

Consider given order of Initialization

            1. If there is a superclass, initialize it first
            2. Static variable declarations and static initializers in the order they appear in the file.
            3. Instance variable declarations and instance initializers in the order they appear in the file.
            4. The constructor.

Rule 1 doesn’t apply because there is no superclass. Rule 2 says to run the static variable declarations and static initializers—in this case, lines 8 and 10, which output 90 and 77. Rule 3 says to run the instance variable declarations and instance initializers—here, line 4, which output 1Z0-808. Finally, rule 4 says to run the constructor—here, line 13, which output constructor.

29
Q

What will be the output of this program?

  class Sup {
           String s = "";
           Sup() {
                   s += "sup ";
           }
  }
  class Sub extends Sup {
           Sub() {
                   s += "sub ";
           }             
   }
   class SubSub extends Sup {
            SubSub() {
                    s += "subsub ";
            }             
            public static void main(String args [ ] ) {
                     System.out.print(new SubSub().s);
            }         
   } 
Please select :
A. subsub sup
B. sup sub subsub 
C. sup subsub
D. subsub sub sup
E. Compilation fails.
A

Option C is the correct answer.

When creating a “SubSub” object, first “SubSub” class constructor is invoked, then its super class “Sup” class constructor is invoked, finally Sup class invoked its super class Object’s constructor. Then it begins to execute remaining statements of constructors. So, here first “sup” will be added to String s, and then “subsub” will be added. So the output will be “sup subsub”. Therefore, the option C is correct.

Options A is incorrect as “sup” will be added before adding “subsub” to String s.

Options B and D are incorrect as “Sub” class constructor is not invoked.

Option E is incorrect since the code compiles fine.

30
Q

What is the output of this program code?

     import java.time.LocalDate;
         public class Whiz {
                     public static void main(String [ ] args) {
                                     LocalDate date = LocalDate.ofYearDay(2015, 363);
                                     date.plusWeeks(2);
                                     System.out.println(date + " : " + date.isLeapYear());
                     }
         }                                                         

Please select :
A. 2016-01-12 : true
B. 2015-12-29 : false
C. An Exception
D. Compilation fails due to an error at line 5.
E. Compilation fails due to multiple errors.

A

Option B is the correct answer.

LocalDate class has few methods which return a LocalDate, such a method is ofYearDay;

public static LocalDate ofYearDay(int year, int dayOfYear)

This method obtains an instance of LocalDate from a year and day-of-year. This returns a LocalDate with the specified year and day-of-year. The day-of-year must be valid for the year; otherwise, an exception will be thrown.

Here at line 5, passing 363 will create LocalDate object with value, 2015-12-29 (since it is not a leap year). At line 6, adding two weeks will not change the date value since LocalDate is immutable. So, finally 2015-12-29 will be printed as the date value, and because it is not a leap year, false will be printed. Hence, option B is correct.

31
Q

Which of the following method can be found in both StringBuilder and String classes?

Please select :
A. concat(String str)
B. isEmpty()
C. charAt(int index)
D. toUpperCase() 
E. None of the above.
A

Option C is the correct answer.

Option C is correct as, among the given methods, only the charAt(int index) method can be found in both classes.

Options A, B, and D are incorrect as these methods are defined in the String class.

32
Q

Which of the following can be filled the blank to print the number of days for the month on line 7?

import java.time.LocalDate;

class Whizlab {
          public static void main(String args[]) {   
                   LocalDate ld  = LocalDate.of(2010,10,10);
                   int days = ld.\_\_\_\_\_\_\_\_\_\_\_\_\_;
                   System.out.println(days);
          }
}
Please select :
A. length();
B. dayCount();
C. lengthOfMonth();
D. sizeOfMonth();
E. days();
A

Option C is the correct answer.

We can use the lengthOfMonth() method to see the number of days for the month in the given LocalDate. So, option C is correct.

public int lengthOfMonth()

The method returns the length of the month represented by this date.This returns the length of the month in days. For example, a date in January would return 31.