Basic Java Online Test Questions and Answers - 2

11. Which of the following can get the number of elements in a one dimensional array arr?

a. arr.size
b. arr.length - 1
c. arr.length  
d. arr.length()

Answer: c. arr.length


12. Which of the following is a Java keyword?

a.  extern
c.  volatile
d.  friend
e.  friendly

Answer: c.  volatile


13. Using which modifier object encapsulates itself from the outside world?

a. private  
b. protected
c. static
d. public

Answer: a. private



14. Which methods can access to private attributes of a class?

a. Only Static methods of the same class
b. Only instances of the same class
c. Only methods those defined in the same class
d. Only classes available in the same package.

Answer: c. Only methods those defined in the same class


15. Which of the following statements is true for constructor?

a. Constructor is called when an instance of the object is created
b. Constructor initializes the object
c. Java compiler creates a default constructor if a class doesn't have any
d. All of the above  

Answer: d. All of the above


16. The constructor of a class has a return type

a. true
b. false

Answer: b. false


17. Which of the following expression is not valid?

a. short s = 23;
b. int num = '7';
c. double d = 1.5;
d. float f = 9.2;

Answer: b. int num = '7';


18. A top level class may have only the following access modifier. Select the one correct answer.

a. package
b. protected
c. private
d. public

Answer: d. public


19. Which of the following is Java keyword?

a. super
b. extern
c. synchronize
d. friend

Answer: a. super


20. Which modifier makes the method available to all classes in the same package and to all the subclasses of this class?

a. public
b. protected
c. private
d. default

Answer: b. protected