Whizbang Practice Exam 2 missed Flashcards

1
Q

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
A

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.

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

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

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.

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

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
A

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.

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

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.

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

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

A

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.

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?

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
A

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.

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

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.

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

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
A

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.

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

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
A

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.

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?

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
A

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.

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?

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
A

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.

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

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

A

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.

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

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

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.

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

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

A

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.

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

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

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.

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

What will be the output of this program?

public class Whiz{

            static int x = 0b1;
            static int y = 0xF;
            static int z = 018;

            public static void main(String args[]){   

                            System.out.println(x+z+y);
            }

}
Please select :
A. 31
B. 32
C. 34
D. Compilation fails due to an error on line 4.
E. Compilation fails due to an error at line 5

A

Option E is the correct answer.

We can use various literal types for numeric type variables, i.e.

            binary literal – 0b11

Octal literal – 012

Decimal literal – 11

Hexadecimal literal – 0xff

At line 5, we have used octal literal, for an octal literal, we can use only 0-7 digits, so using 8 will result in compile time error. Hence option E is correct.

17
Q

How many objects are eligible for GC when line 10 is reached?

class Wrap {
         Double d = 10.0;
         int x = 10;
         int [ ] s = new int[10];
}
public class Whiz {
         public static void main(String [] args){
                  Wrap w =new Wrap();
                  w = null;
         }
}
Please select :
A. 1
B. 2
C. 3
D. 4 
E. Compilation fails
A

Option C is the correct answer.

At line 8, we have created a wrap instance, in that wrapper, there are two objects, one is an array of ints and other is a Double. So, making w reference null will result in Wrap object unreachable and also its enclosing two objects. Hence, total three objects eligible for GC. Hence, option C is correct.

18
Q

Which of the following will print true?

Double d = 10.0;
int i = 10;
Integer wi = 10;

Please select :
A. System.out.print((wi == d));
B. System.out.print(d == i);
C. System.out.print(d.equals(i));
D. System.out.print(d.equals(wi));
E. System.out.print(wi.equals(d));
A

Option B is the correct answer.

Option B is correct since it returns true. Comparison of Wrapper and primitive using “==” will unwrap the wrapper to primitive and compare, hence “d==I” return true.

Option A is incorrect since it results in a compile-time error because we can’t use “==” with two different wrappers.

Other options are incorrect since equals method of the wrapper will first check if the wrapper is of the same type, hence others will return false.

19
Q

What will be the output of this program?

       class Whiz{
                       public static void main(String args[]){
                            A ab = new B();
                            ab.print();
                            ab.print("C");
                       }
       }
       class A{
                   public void print(){
                                   System.out.print("A");
                   }
   }
   class B extends A{
                   public void print(String s){
                                   System.out.print(s);
                   }
   }                             
Please select :
A. AA
B. CC
C. AC
D. An exception is thrown 
E. Compilation fails
A

Option E is the correct answer.

When compiling the code, Compiler looks only at the reference and sees that A doesn’t have a print() method that takes a String. The compiler doesn’t care that the actual object might be a B at runtime. So, it will result in a compile-time error, so option E is correct.

20
Q

What will be the output of this program?

   class Whiz {
             public static void main(String args[]) {
                      String s = "1Z";
                      s.concat("0");
                      s += "1";
                      System.out.println(s + "-808");
           }
   }
Please select :
A. 1Z01-808
B. 1Z0-808
C. 1Z1-808
D. An Exception is thrown at runtime
E. Compilation fails
A

Option C is the correct answer.

Strings are immutable, so at line 4 concatenating “0” will not add “0” to s, instead it will return new String with value “1Z0”. At line 5, using assignment operator will add “1” to s, so output will be 1Z1-808. Hence, option C is correct.