- Home
- Programming
- Java SE7
1.
Given the following definition of the classes Animal , Lion , and Jumpable ,
select the correct combinations of assignments of a variable (select 2 options):
interface Jumpable {}
class Animal {}
class Lion extends Animal implements Jumpable {}
interface Jumpable {}
class Animal {}
class Lion extends Animal implements Jumpable {}
- A.Jumpable var1 = new Jumpable();
- B.Animal var2 = new Animal();
- C.Lion var3 = new Animal();
- D.Jumpable var4 = new Animal();
- E.Jumpable var5 = new Lion();
- Answer & Explanation
- Report
Answer : [B, E]
Explanation :
Explanation :
Option (a) is incorrect. An interface can’t be instantiated. Option (c) is incorrect. A reference variable of a derived class can’t be used to refer to an object of its base class. Option (d) is incorrect. A reference variable of type Jumpable can’t be used to refer to an object of the class Animal because Animal doesn’t implement the interface Jumpable . |
2.
Which of the following statements are true? (Select 3 options.)
- A.A Java class can define multiple methods.
- B.A Java class can define multiple variables.
- C.A Java class can be defined in multiple packages
- D.A Java class can import multiple packages.
- E.A Java class can’t define more than 108 constructors.
- F.End-of-line comments can’t follow import or package statements.
- G.Multiline comments can only be defined within a method definition.
- Answer & Explanation
- Report
Answer : [A, B, D]
Explanation :
Explanation :
Option (c) is incorrect. The same class can’t be defined in multiple
packages. If you try to define the same class—say, the class Person —in the packages
com.ejava and com.eoracle , you’re defining two classes with the same name but in
separate packages. In this case, com.ejava.Person and com.eoracle.Person will refer
to two different classes. Option (e) is incorrect because there is no theoretical limit on the number of con- structors that can be defined by a class. Option (f) is incorrect because end-of-line comments can follow any line of code. Option (g) is incorrect because multiline comments can also be placed outside a method definition. |
3.
Given the following code, which option, if used to replace /* INSERT CODE HERE */ , will make the code print 1 ? (Select 1 option.)
try {
String[][] names = {{"Andre", "Mike"}, null, {"Pedro"}};
System.out.println(names[2][1].substring(0, 2));
}
catch (/* INSERT CODE HERE */) {
System.out.println(1);
}
try {
String[][] names = {{"Andre", "Mike"}, null, {"Pedro"}};
System.out.println(names[2][1].substring(0, 2));
}
catch (/* INSERT CODE HERE */) {
System.out.println(1);
}
- A.IndexPositionException e
- B.NullPointerException e
- C.ArrayIndexOutOfBoundsException e
- D.ArrayOutOfBoundsException e
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
Explanation: Options (a) and (d) are incorrect because the Java API doesn’t define
any exception classes with these names. Here’s a list of the array values that are initialized by the code in this question: names[0][0] = "Andre" names[0][1] = "Mike" names[1] = null names[2][0] = "Pedro" Because the array position [2][1] isn’t defined, any attempt to access it will throw ArrayIndexOutOfBoundsException . An attempt to access any position of the second array—that is, names[1][0] —will throw NullPointerException because names[1] is set to null . |
4.
What is the output of the following code? (Select 1 option.)
int a = 10; String name = null;
try {
a = name.length();
a++;
}
catch (RuntimeException e){
++a;
}
System.out.println(a);
int a = 10; String name = null;
try {
a = name.length();
a++;
}
catch (RuntimeException e){
++a;
}
System.out.println(a);
- A.5
- B.6
- C.10
- D.11
- E.12
- F.Runtime exception
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
Because the variable name isn’t assigned a value, the following line of
code will throw NullPointerException : name.length(); Hence, the original value of the variable a isn’t modified and the control is trans- ferred to the exception handler, which increments the value of the variable a to 11 . |
5.
Given the following class definition,
what is the output of the following code? (Select 1 option.)
class Result {
public static void main(String... args) {
Student s = new Student();
switch (s.marks) {
default: System.out.println("100");
case 10: System.out.println("10");
case 98: System.out.println("98");
}
}
}
what is the output of the following code? (Select 1 option.)
class Result {
public static void main(String... args) {
Student s = new Student();
switch (s.marks) {
default: System.out.println("100");
case 10: System.out.println("10");
case 98: System.out.println("98");
}
}
}
- A.100
10
98 - B.10
98 - C.100
- D.10
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
The default case executes only if no matching values are found. In this case, a matching value of 10 is found and the case label prints 10 . Because a break statement doesn’t terminate this case label, the code execution continues and executes the remaining statements within the switch block, until a break state- ment terminates it or it ends. |