Java OCA OCP Practice Question 2566

Question

Given:

1. public class Main {  
2.   public static void main(String[] args) {  
3.     // insert code here  
4.   }  /*from w  ww. j ava2 s.  c  o  m*/
5. }  
6. class MyClass {  
7.   enum Members {   
8.     Dev(48), Tester(74), Manager(50);  
9.     int height;  
10.     Members(int h) { height = h; }  
11.     int getHeight() { return height; }  
12.   }  
13. } 

And these four lines of code to be inserted, independently at line 3:

  • I. int h0 = MyClass.Members.Dev.getHeight();
  • II. int h1 = MyClass.Members.getHeight();
  • III. int h2 = Members.Dev.getHeight();
  • IV. int h3 = Members.height;

Which are true? (Choose all that apply.)

  • A. Line I will compile.
  • B. Line II will compile.
  • C. Line III will compile.
  • D. Line IV will compile.
  • E. Class MyClass will NOT compile.


A

Note

A is the correct syntax to access the height variable.

B, C, and D are incorrect syntax, and E is incorrect because enums can have variables, constructors, and methods.




PreviousNext

Related