Witscale Test Center

Sample test


Sample test

2.            Consider the following code and fill in the blanks.

 

byte b = 10;

char c = ‘a’;

_____ summation = b + c;

 

Which of the following types can be used as the type of summation? Select all correct answers.

 

A.    void

B.    byte

C.    char

D.    short

E.    int

F.    float

G.    null

 

 

2. Which of the following statements are true about type conversion and casting?

 

A.      Whenever you change a type of an object reference, you must use the cast operator.

B.      Primitives never require cast operator. However, it is a good practice to use cast operator to make the code more readable.

C.      Arithmetic promotion does not take place when you use object references as operands.

D.     Casting of object references may throw a runtime ClassCastException.

E.      Casting of primitives may throw ClassCastException at runtime.

 

 

3.  Consider the following code. (Next question is also based on this code)

 

                1.            public class Tester {

                2.             

                3.               private void increment(int a) {

                4.                a++;

                5.                System.out.print(" int ");

                6.               }

                7.             

                8.               private void increment(byte a) {

                9.                a += 2;

            10.             System.out.print(" byte ");

            11.             }

            12.             

            13.             private void increment(char a) {

            14.              a += 3;

            15.              System.out.print(" char ");

            16.             }

            17.             

            18.             public static void main(String[] args) {

            19.              Tester tester = new Tester();

            20.              byte b = 6;

            21.              System.out.print(b);

            22.              tester.increment(b);

            23.              System.out.print(b);

            24.             }

            25.            }

 

What will be the output after the code is executed?

 

A.    6 int 7

B.    6 byte 8

C.    6 byte 6

D.    6 char 9

E.    6 int 6

 

 

4.    What will be the output if we first remove the code containing increment method (that takes byte argument ) from line 8 to line 11 and then execute the code?

 

A.      The code will not compile

B.      The code will compile but fail at runtime with exception NoSuchMethod at line 22.

C.      6 char 9

D.     6 int 6

E.      6 int 7

 

 

5.    Consider the following code.

 

                1.             public class Tester {

                2.             

                3.               public static void main(String[] args) {

                4.                 int i = 11;

                5.                 int j = (i%2 == 0) ? 121.0 : i--;

                6.                 System.out.println("i = " + i  + " j = " + j);

                7.               }

                8.             }

 

Which of the following statements are true about the above class.

 

A.      i = 11 j = 0 is printed after execution.

B.      i = 11 j = 121 is printed after execution.

C.      i = 11 j = 121.0 is printed after execution.

D.     i = 11 j = 11 is printed after execution.

E.      This code will fail to compile at line 5.



 

5.       Consider the following code.

 

                1.            interface Mowable {}

                2.            class Grass implements Mowable {}

                3.            class TurfGrass extends Grass {}

                4.            class Weed implements Mowable {}

                5.             

                6.            public class Tester {

                7.                 

                8.                private static void mow(Mowable mowablething) {

                9.                     // mow it

            10.                }

            11.               public static void main(String[] args) {

            12.                Grass grass = new Grass();

            13.                TurfGrass turfGrass = new TurfGrass();

            14.                Weed weed = new Weed();

            15.                

            16.               }

            17.            }

 

Which of the following calls can be placed at line 15 and the code will still compile and run.

 

A.    mow(grass);

B.    mow(turfGrass);

C.    mow(weed);

D.    mow(new Mowable());

E.    mow(“Grass”);

F.    None of the above

 

 

7. Consider the following code.

 

                1.             

                2.            interface Mowable {}

                3.            class Grass implements Mowable {}

                4.            class TurfGrass extends Grass {}

                5.            class Weed implements Mowable {}

                6.             

                7.             public class Tester {

                8.             

                9.                 public static void main(String[] args) {

            10.                   Grass grass = new Grass();

            11.                   TurfGrass turfGrass = new TurfGrass();

            12.                   Weed weed = new Weed();

            13.                   grass = turfGrass;

            14.                   Object o = weed;

            15.                   Mowable mowableThing = weed;

            16.                   grass = mowableThing;

            17.              }

            18.            }

 

 

Which of the following is true about the given code?

 

A.      The code will fail to compile at line 11.

B.      The code will fail to compile at line 12.

C.      The code will fail to compile at line 13.

D.     The code will fail to compile at line 14.

E.      This code will compile and run without any problem.

 

 

8.    Consider the following code.

 

                1.            interface Mowable {}

                2.            class Grass implements Mowable {}

                3.            class TurfGrass extends Grass {}

                4.            class Weed implements Mowable {}

                5.             

                6.               public class Tester {

                7.                 public static void main(String[] args) {

                8.                  Grass grass = new Grass();

                9.                  Mowable m = grass;

            10.                TurfGrass turfGrass = (TurfGrass) grass;

            11.              }

            12.            }

 

 

Which of the following statements are true about this code?

 

A.      This code will fail to compile at line 9. An explicit cast to Mowable as Mowable m = (Mowable) grass; can get it working.

B.      This code will compile but throw a ClassClastException at line 10.

C.      This code compiles, it will still compile if the cast operator is removed.

D.     This code will compile but throw a ClassClastException at line 9.

 

 

9.   Consider the following code.

 

                1.              public class Tester {

                2.                public static void main(String[] args) {

                3.                  double[] prices = new double[10];

                4.                  int i = 5;

                5.                  long wholesalePrice = 100L;

                6.                  prices[i] = wholesalePrice;

                7.                  Object o = prices;

                8.                  float[] costs = (float[]) o;

                9.                  costs[i] = wholesalePrice;

            10.             }

            11.           

 

Which of the following statements are true about this code?

 

A.      This code will fail to compile at line 6 as long value cannot be store in double array.

B.      This code will fail to compile at line 7.

C.      This code will fail to compile at line 8 as object of Object type cannot be stored in float array.

D.     This code will fail to compile at line 9 as long value cannot be store in float array.

E.      This code will compile but throw a ClassClastException at line 8.

F.       This code will compile but throw a ClassClastException at line 9.

 

 

10.  Consider the following code.

 

                            1.              public class ArgumentPass {

                            2.                void processIt(float[] arr) {

                            3.                  for (int i = 0; i < arr.length; i++) {

                            4.                    arr[i] = 10;

                            5.                  }

                            6.                }

                            7.             

                            8.                public static void main(String[] args) {

                            9.                 float[] prices = { 1.99f, 2.99f, 3.49f };

                        10.               ArgumentPass p = new ArgumentPass();

                        11.               p.processIt(prices);

                        12.               for (int i = 0; i < prices.length; i++) {

                        13.                 System.out.print(prices[i] + " ");

                        14.               }

                        15.             }

                        16.             

                        17.            }

 

 

What will be printed after this code is executed?

 

A.      3.49

B.      10.0 10.0 3.49

C.      10.0 10.0 10.0

D.     1.99 2.99 3.49 10.0 10.0 10.0

E.      Nothing. The code will fail to compile.