- Home
- Programming
- Java SE7
6.
Given the following code, which code can be used to create and initialize an object of class ColorPencil ? (Select 2 options.)
class Pencil {}
class ColorPencil extends Pencil {
String color;
ColorPencil(String color) {this.color = color;}
}
class Pencil {}
class ColorPencil extends Pencil {
String color;
ColorPencil(String color) {this.color = color;}
}
- A.ColorPencil var1 = new ColorPencil();
- B.ColorPencil = new ColorPencil(RED);
- C.ColorPencil = new ColorPencil("RED");
- D.Pencil var 4 = new ColorPencil("BLUE");
- Answer & Explanation
- Report
Answer : [C, D]
Explanation :
Explanation :
Option (a) is incorrect because new ColorPencil() tries to invoke the no-argument constructor of class ColorPencil , which isn’t defined in class ColorPencil . Option (b) is incorrect because new ColorPencil(RED) tries to pass a variable RED, which isn’t defined in the code. |
7.
What is the output of the following code? (Select 1 option.)
class Doctor { protected int age; protected void setAge(int val) { age = val; } protected int getAge() { return age; } } class Surgeon extends Doctor { Surgeon(String val) { specialization = val; } String specialization; String getSpecialization() { return specialization; } } class Hospital { public static void main(String args[]) { Surgeon s1 = new Surgeon("Liver"); Surgeon s2 = new Surgeon("Heart"); s1.age = 45; System.out.println(s1.age + s2.getSpecialization()); System.out.println(s2.age + s1.getSpecialization()); } }
- A.45Heart
0Liver - B.45Liver
0Heart - C.45Liver
45Heart - D.45Heart
45Heart - E.Class fails to compile.
- Answer & Explanation
- Report
Answer : [A]
Explanation :
Explanation :
The constructor of the class Surgeon assigns the values "Liver" and "Heart" to the variable specialization of objects s1 and s2 . The variable age is protected in the class Doctor . Also, the class Surgeon extends the class Doctor . Hence, the variable age is accessible to reference variables s1 and s2 . The code assigns a value of 45 to the member variable age of reference variable s1 . The variable age of reference variable s2 is initialized to the default value of an int , which is 0 . Hence, the code prints the values mentioned in option (a). |
8.
What is the output of the following code? (Select 1 option.)
class RocketScience {
public static void main(String args[]) {
int a = 0;
while (a == a++) {
a++;
System.out.println(a);
}
}
}
class RocketScience {
public static void main(String args[]) {
int a = 0;
while (a == a++) {
a++;
System.out.println(a);
}
}
}
- A.The while loop won’t execute; nothing will be printed.
- B.The while loop will execute indefinitely, printing all numbers, starting from 1 .
- C.The while loop will execute indefinitely, printing all even numbers, starting from 0 .
- D.The while loop will execute indefinitely, printing all even numbers, starting from 2 .
- E.The while loop will execute indefinitely, printing all odd numbers, starting from 1 .
- F.The while loop will execute indefinitely, printing all odd numbers, starting from 3 .
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
The while loop will execute indefinitely because the condition a == a++ will always evaluate to true . The postfix unary operator will increment the value of the variable a after it’s used in the comparison expression. a++ within the loop body will increment the value of a by 1 . Hence, the value of a increments by 2 in a single loop. |
9.
Given the following statements,
-> com.ejava is a package
-> class Person is defined in package com.ejava
-> class Course is defined in package com.ejava
which of the following options correctly import the classes Person and Course in the class MyEJava ? (Select 3 options.)
-> class Person is defined in package com.ejava
-> class Course is defined in package com.ejava
which of the following options correctly import the classes Person and Course in the class MyEJava ? (Select 3 options.)
- A.import com.ejava.*;
class MyEJava {} - B.import com.ejava;
class MyEJava {} - C.import com.ejava.Person;
import com.ejava.Course; class MyEJava {} - D.import com.ejava.Person;
import com.ejava.*; class MyEJava {}
- Answer & Explanation
- Report
Answer : [A, C, D]
Explanation :
Explanation :
Option (a) is correct. The statement import com.ejava.*; imports all
the public members of the package com.ejava in class MyEJava .
Option (b) is incorrect. Because com.ejava is a package, to import all the classes
defined in this package, the package name should be followed by .* :
import com.ejava.*; Option (c) is correct. It uses two separate import statements to import each of the classes Person and Course individually, which is correct. Option (d) is also correct. The first import statement imports only the class Person in MyClass . But the second import statement imports both the Person and Course classes from the package com.ejava . You can import the same class more than once in a Java class with no issues. This code is correct. In Java, the import statement makes the imported class visible to the Java compiler, allowing it to be referred to by the class that’s importing it. In Java, the import state- ment doesn’t embed the imported class in the target class. |
10.
Given that the following classes Animal and Forest are defined in the same
package, examine the code and select the correct statements (select 2 options):
line1> line2> line3> line4> line5> class Animal { public void printKing() { System.out.println("Lion"); } } line6> line7> line8> line9> line10> line11> class Forest { public static void main(String... args) { Animal anAnimal = new Animal(); anAnimal.printKing(); } }
- A.The class Forest prints Lion .
- B.If the code on line 2 is changed as follows, the class Forest will print Lion :
private void printKing() { - C.If the code on line 2 is changed as follows, the class Forest will print Lion :
void printKing() { - D.If the code on line 2 is changed as follows, the class Forest will print Lion :
default void printKing() {
- Answer & Explanation
- Report
Answer : [A, C]
Explanation :
Explanation :
Option (a) is correct. The code will compile successfully and print Lion .
Option (b) is incorrect. The code won’t compile if the access modifier of the
method printKing is changed to private . private members of a class can’t be
accessed outside the class. Option (c) is correct. The classes Animal and Forest are defined in the same pack- age, so changing the access modifier of the method printKing to default access will still make it accessible in the class Forest . The class will compile successfully and print Lion . Option (d) is incorrect. “default” isn’t a valid access modifier or keyword in Java. In Java, the default accessibility is marked by the absence of any explicit access modifier. This code will fail to compile |