Witscale Test Center

Sample test


Sample test

This test assesses your understanding of concepts explained in this chapter. Please read all question carefully. The questions may have multiple answers. Choose all the correct answers.

 

1.    Which of the following declarations are not valid? (Choose all the correct answers)

A.    transient String defaultMessage = "Good Morning";

B.    abstract int pi;

C.    public final static native char lineFeed();

D.    private abstract travel();

E.    default int sum; 

 

2.    Which of the following statements are true about the given classes?

 

1.       package com.example.birds.Sanctuary;

2.        class Bird {

3.         static int birdCount;

4.         public Bird() {

5.           birdCount++;

6.         }

7.        }

8.        

9.       public class StaticVariableTest {

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

11.         Bird little1 = new Bird();

12.         Bird little2 = new Bird();

13.         Bird.birdCount++;

14.         little1 = new Bird();

15.         System.out.println(Bird.birdCount);

16.     }

17.    }

 

A.      The code will compile and execute. The output will be 1.

B.      The code will compile and execute. The output will be 3.

C.      The code fails to compile as constructor of class Bird refers to a static variable.

D.     The code will compile and execute. The output will be 4.

E.      The code will compile and execute. The output will be 0.

F.       The code fails to compile because the class Bird is accessing variable birdCount without creating any instance.

 

3.    Which of the following statements is true about the given class?

  

1.       package com.example.scjp.studykit;

2.       public class StaticTest {

3.         static int s;

4.         static {

5.          s = s + 5;

6.         }

7.         

8.         public StaticTest() {

9.          s = s + 5;

10.      }

11.      

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

13.        System.out.println(s);

14.      }

15.     

16.      static {

17.       s = s + 10;

18.      }

19.     }

 

A.      The code fails to compile, as static variable s is not initialized properly.

B.      The code will compile and execute. The output will be 10.

C.      The code will compile and execute. The output will be 5.

D.     The code will compile and execute. The output will be 20.

E.      The code will compile and execute. The output will be 15.

F.       The code fails to compile at line 13 as a class can have only one static initializer block.

 

4.    Which of the following statements is true?

 

A.      A final class cannot have any abstract method.

B.      An abstract class cannot have any final method.

C.      A final class cannot have any native method.

 

5.    Which of the following statements are true?

 

A.      Static variable’s value cannot be changed.

B.      Transient methods cannot be overridden.

C.      Final member variables must be initialized.

D.     Native class is implemented in native library

E.      Transient variables are not serialized

 

6.    Which of the following statements is true about the given classes in file Cat.java

 

1.       package com.example.scjp.sampletests;

2.        public class Cat {

3.          protected static String family;

4.          public Cat() {

5.           family = "feline";

6.          }

7.          protected void move() {

8.          }

9.        }

10.     

11.     class HouseCat extends Cat {

12.       protected void move() {

13.         // how the cat moves    

14.       }

15.     }

16.     

17.     class Lion extends Cat {

18.       public Lion() {

19.        family = "BigCat";

20.       }

21.       protected void move() {

22.        // how the lion moves    

23.       }

24.     }

                                                                  

A.      Compilation of Lion class fails at line 19, as move() is a protected method in Cat class.

B.      The value of the variable family before creation of any Cat, HouseCat or Lion object is null.

C.      Compilation of Lion class fails as it is not importing the Cat class before using it.

D.     Compilation of Cat class fails as it is accessing a protected static variable in constructor.

E.      The value of variable family for all Lion objects will always be “BigCat”.

 

7.    Which of the following statements is true about the classes above and the class below?

 

1.       package com.example.scjp.sampletests;

2.        public class Tester {

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

4.          Cat cat = new HouseCat();

5.          System.out.println("HouseCat belongs to the " + cat.family +

6.          " family.");

7.          Cat.family = “feline”;

8.          cat = new Lion();

9.          System.out.println("Lion belongs to the " + cat.family +

10.       " family.");

11.       }

12.    }

 

A.     The code will compile and execute. The output will be

HouseCat belongs to the feline family.

Lion belongs to the BigCat family.

 

B.     The code will compile and execute. The output will be

HouseCat belongs to the feline family.

Lion belongs to the feline family.

 

C.     The code will compile and execute. The output will be

HouseCat belongs to the null family.

Lion belongs to the BigCat family.

 

D.     The code will fail to compile because the protected static variable family is not accessible from class Tester.

 

 

8.    Which of the following are valid constructors for class Point? (Choose all the correct answers)

 

A.      void Point() { }

B.      private final point() { }

C.      abstract Point() { }

D.     Point(Point newPoint) { }

E.      point() { }

F.       Point() { }

G.     private Point() { }