Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. What happens in the Java Virtual Machine (JVM) when the following line is pro

ID: 3886434 • Letter: 1

Question

1. What happens in the Java Virtual Machine (JVM) when the following line is processed?
MyClass n = new MyClass();
A. Nothing, the line is skipped, since no parameters are defined
B. An object of type MyClass is created, no reference is created
C. A reference to an object of type MyClass is created, no object is created
D. Both a reference and an object of type MyClass are created
2. Which of the following is true?
A. A single reference variable can refer to multiple objects at one time
B. Multiple reference variables can refer to the same, single object
C. An object can only be referred to by a single reference variable
D. A reference variable always refers to a valid object
3. The class "Parent" and its subclass "Child" both implement a method with header: "public String printName()", printing "parent" and "child" respectively. Which of the 4 choices below reflects the correct output of the following program:
Parent v1 = new Parent();
Child v2 = new Child();
Parent v3 = new Child();
System.out.println(v1.printName()+" "+ v2.printName()+" "+ v3.printName());
A. parent child child
B. parent child parent
C. parent parent parent
D. child parent child
4. For the program in Q3 above, what are the types of variable v3 at the compilation time and at the run time?
A. Parent Parent
B. Parent Child
C. Child Parent
D. Child Child
5. In the programs in Q3 above, is the variable v2 a reference variable?
A. Yes
B. No
6. For the program in Q3 above, if the class “Child” implements another method with header: “public void display()”. And the class “Child” wants to call the method “printName()” in the class “Parent” from its own method “public void display()”, which of the following is correct?
A. printName()
B. this.printName()
C. Parent.printName()
D. super.printName()

Explanation / Answer

1. What happens in the Java Virtual Machine (JVM) when the following line is processed? MyClass n = new MyClass(); A. Nothing, the line is skipped, since no parameters are defined B. An object of type MyClass is created, no reference is created C. A reference to an object of type MyClass is created, no object is created D. Both a reference and an object of type MyClass are created D. Both a reference and an object of type MyClass are created (self explanatory) MyClass n --> represents reference new MyClass() --> represents object 2. Which of the following is true? A. A single reference variable can refer to multiple objects at one time False - it can refer only one variable at one time B. Multiple reference variables can refer to the same, single object True - Multiple reference can refer to same/single object C. An object can only be referred to by a single reference variable False - object can be referred by multiple reference variables D. A reference variable always refers to a valid object False - May refere to valid object or it may not refer to any object 3. The class "Parent" and its subclass "Child" both implement a method with header: "public String printName()", printing "parent" and "child" respectively. Which of the 4 choices below reflects the correct output of the following program: Parent v1 = new Parent(); //Parent ref to parent obj Child v2 = new Child(); //child ref to child object Parent v3 = new Child(); //parent ref to child object System.out.println(v1.printName()+" "+ v2.printName()+" "+ v3.printName()); A. parent child child parent -> printed by parent ref and object child -> printed by child ref and child object child -> printed by parent ref but child object(parent class method is overridden) B. parent child parent C. parent parent parent D. child parent child 4. For the program in Q3 above, what are the types of variable v3 at the compilation time and at the run time? A. Parent Parent B. Parent Child Compile time Parent (Since compiler does not have any idea what happens at runtime. Runtime time Child C. Child Parent D. Child Child 5. In the programs in Q3 above, is the variable v2 a reference variable? A. Yes(solution basics) B. No 6. For the program in Q3 above, if the class “Child” implements another method with header: “public void display()”. And the class “Child” wants to call the method “printName()” in the class “Parent” from its own method “public void display()”, which of the following is correct? A. printName() B. this.printName() C. Parent.printName() D. super.printName() -> In java super keyword is used to call the variable and methods of parent class