- Home
- Programming
- Java SE7
31.
Ideally, which of the following should never be handled by an exception handler? (Select 2 options.)
- A.StackOverflowError
- B.OutOfMemoryError
- C.ArrayIndexOutOfBoundError
- D.ClassLoadingException
- E.CompilationError
- F.OutOfStorageError
- Answer & Explanation
- Report
Answer : [A, B]
Explanation :
Explanation :
Options (c), (d), (e), and (f) are incorrect because the Java API doesn’t
define these exception or error classes. Options (a) and (b) are correct. You should never try to handle these errors in your code because StackOverflowError and OutOfMemoryError are serious errors. |
32.
What is the output of the following code? (Select 1 option.)
public class MyCalendar {
public static void main(String arguments[]) {
Season season1 = new Season();
season1.name = "Spring";
Season season2 = new Season();
season2.name = "Autumn";
season1 = season2;
System.out.println(season1.name);
System.out.println(season2.name);
}
}
class Season {
String name;
}
public class MyCalendar {
public static void main(String arguments[]) {
Season season1 = new Season();
season1.name = "Spring";
Season season2 = new Season();
season2.name = "Autumn";
season1 = season2;
System.out.println(season1.name);
System.out.println(season2.name);
}
}
class Season {
String name;
}
- A.String
Autumn - B.Spring
String - C.Autumn
Autumn - D.Autumn
String
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
Multiple variable references can point to the same object. The following
lines of code define a reference variable season1 , which refers to an object that has
the value of its instance variable ( name ) set to Spring : Season season1 = new Season(); season1.name = "Spring"; The following lines of code define a reference variable season2 , which refers to an object that has the value of its instance variable ( name ) set to Autumn : Season season2 = new Season(); season2.name = "Autumn"; The following line of code reinitializes the reference variable season1 and assigns it to the object referred to by the variable season2 : season1 = season2; Now the variable season1 refers to the object that is also referred to by the variable season2 . Both of these variables refer to the same object—the one that has the value of the instance variable set to Autumn . Hence, the output of the previous code is as follows: Autumn Autumn |
33.
What is true about the following code? (Select 1 option.)
class Shoe {}
class Boot extends Shoe {}
class ShoeFactory {
ShoeFactory(Boot val) {
System.out.println("boot");
}
ShoeFactory(Shoe val) {
System.out.println("shoe");
}
}
class Shoe {}
class Boot extends Shoe {}
class ShoeFactory {
ShoeFactory(Boot val) {
System.out.println("boot");
}
ShoeFactory(Shoe val) {
System.out.println("shoe");
}
}
- A.The class ShoeFactory has a total of two overloaded constructors.
- B.The class ShoeFactory has three overloaded constructors, two user-defined constructors, and one default constructor.
- C.The class ShoeFactory will fail to compile.
- D.The addition of the following constructor will increment the number of constructors of the class ShoeFactory to 3:
private ShoeFactory (Shoe arg) {}
- Answer & Explanation
- Report
Answer : [A]
Explanation :
Explanation :
Java accepts changes in the objects of base-derived classes as the sole cri-
terion to define overloaded constructors and methods. Option (b) is incorrect because Java doesn’t generate a default constructor for a class that has already defined a constructor. Option (c) is incorrect. All classes defined for this example compile successfully. Option (d) is incorrect. The class ShoeFactory already defines a constructor that accepts a method argument of type Shoe . You can’t overload a constructor with a mere change in its access modifier. |
34.
Given the following definitions of the classes ColorPencil and TestColor ,
which option, if used to replace /* INSERT CODE HERE */ , will initialize the instance
variable color of reference variable myPencil with the String literal value "RED" ?
(Select 1 option.)
class ColorPencil {
String color;
ColorPencil(String color) {
//INSERT CODE HERE
}
}
class TestColor {
ColorPencil myPencil = new ColorPencil("RED");
}
class ColorPencil {
String color;
ColorPencil(String color) {
//INSERT CODE HERE
}
}
class TestColor {
ColorPencil myPencil = new ColorPencil("RED");
}
- A.this.color = color;
- B.color = color;
- C.color = RED;
- D.this.color = RED;
- Answer & Explanation
- Report
Answer : [A]
Explanation :
Explanation :
Option (b) is incorrect. This line of code will assign the value of the
method parameter to itself. The constructor of the class ColorPencil defines a method
parameter with the same name as its instance variable, color . To access an instance variable in the constructor, it must be prefixed with the keyword this , or it will refer to the method parameter color . Options (c) and (d) are incorrect. They try to access the value of variable RED , which isn’t defined in the code. |
35.
What is the output of the following code? (Select 1 option.)
class EJavaCourse {
String courseName = "Java";
}
class University {
public static void main(String args[]) {
EJavaCourse courses[] = { new EJavaCourse(), new EJavaCourse() };
courses[0].courseName = "OCA";
for (EJavaCourse c : courses) c = new EJavaCourse();
for (EJavaCourse c : courses) System.out.println(c.courseName);
}
}
class EJavaCourse {
String courseName = "Java";
}
class University {
public static void main(String args[]) {
EJavaCourse courses[] = { new EJavaCourse(), new EJavaCourse() };
courses[0].courseName = "OCA";
for (EJavaCourse c : courses) c = new EJavaCourse();
for (EJavaCourse c : courses) System.out.println(c.courseName);
}
}
- A.Java
Java - B.OCA
Java - C.OCA
OCA - D.None of the above.
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
This question tests you on multiple concepts: how to read from and
write to object fields, how to use arrays, the enhanced for loop, and assigning a value
to a loop variable. The code defines an array of the class EJavaCourse with two elements. The default value of the variable courseName — Java —is assigned to each of these two elements. courses[0].courseName = "OCA" changes the value courseName , for the object stored at array position 0 . c = new EJavaCourse() assigns a new object to the loop variable c . This assignment doesn’t reassign new objects to the array reference variables. System.out.println(c.courseName) prints the name of the courseName of the objects initially stored by the array, using the loop variable c . The loop variable in the enhanced for loop refers to a copy of the array or list element. If you modify the state of the loop variable, the modified object state will be reflected in the array. But if you assign a new object to the loop variable, it won’t be reflected in the list or the array that’s being iterated. You can compare this behavior of the enhanced for loop variable with the behavior of object references passed as arguments to a method. |