- Home
- Programming
- Java SE7
36.
Given the following code, which option, if used to replace /* INSERT CODE HERE
*/ , will make the code print the value of the variable screenSize ? (Select 1 option.)
class Tablet {
float screenSize = 7.0f;
float getScreenSize() {
return screenSize;
}
void setScreenSize(float val) {
screenSize = val;
}
}
class DemoTabs {
public static void main(String args[]) {
Tablet tab = new Tablet();
System.out.println(/* INSERT CODE HERE */);
}
}
class Tablet {
float screenSize = 7.0f;
float getScreenSize() {
return screenSize;
}
void setScreenSize(float val) {
screenSize = val;
}
}
class DemoTabs {
public static void main(String args[]) {
Tablet tab = new Tablet();
System.out.println(/* INSERT CODE HERE */);
}
}
- A.tab.screenSize
- B.tab->getScreensize()
- C.tab::getScreen()
- D.tab:screenSize
- Answer & Explanation
- Report
Answer : [A]
Explanation :
Explanation :
Only the dot operator ( . ) can be used to access an instance variable or a method of an object in Java. The rest of the operators ( -> , :: , and :) used in options (b), (c), and (d), respectively, aren’t valid operators in Java. |
37.
Given the following definitions of the class Person and the interface
Movable , the task is to declare a class Emp that inherits from the class Person and
implements the interface Movable . Select the correct option to accomplish this task
(choose 1 option):
class Person {}
interface Movable {}
class Person {}
interface Movable {}
- A.class Emp implements Person extends Movable{}
- B.class Emp implements Person, Movable{}
- C.class Emp extends Person implements Movable{}
- D.class Emp extends Person, Movable{}
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
Options (a) and (b) are incorrect because a class can’t use the keyword
implements to inherit a class . Option (d) is incorrect because a class can’t use the keyword extends to inherit an interface . |
38.
What is the output of the following code? (Select 1 option.)
class Phone {
static void call() {
System.out.println("Call-Phone");
}
}
class SmartPhone extends Phone{
static void call() {
System.out.println("Call-SmartPhone");
}
}
class TestPhones {
public static void main(String... args) {
Phone phone = new Phone();
Phone smartPhone = new SmartPhone();
phone.call();
smartPhone.call();
}
}
class Phone {
static void call() {
System.out.println("Call-Phone");
}
}
class SmartPhone extends Phone{
static void call() {
System.out.println("Call-SmartPhone");
}
}
class TestPhones {
public static void main(String... args) {
Phone phone = new Phone();
Phone smartPhone = new SmartPhone();
phone.call();
smartPhone.call();
}
}
- A.Call-Phone
Call-Phone - B.Call-Phone
Call-SmartPhone - C.Call-Phone
null - D.null
Call-SmartPhone
- Answer & Explanation
- Report
Answer : [A]
Explanation :
Explanation :
Invocation of a static method is tied to the type of the reference variable
and doesn’t depend on the type of the object that’s assigned to the reference variable.
The static method belongs to a class, not to its objects. Re-examine the following code: Phone smartPhone = new SmartPhone(); smartPhone.call(); In the preceding code, the type of the reference variable smartPhone is Phone . Because call is a static method, smartPhone.call() calls the method call defined in the class Phone . |
39.
Given the following code, which of the following statements are true? (Select 3 options.)
class MyExam {
void question() {
try {
question();
}
catch (StackOverflowError e) {
System.out.println("caught");
}
}
public static void main(String args[]) {
new MyExam().question();
}
}
class MyExam {
void question() {
try {
question();
}
catch (StackOverflowError e) {
System.out.println("caught");
}
}
public static void main(String args[]) {
new MyExam().question();
}
}
- A.The code will print caught .
- B.The code won’t print caught .
- C.The code would print caught if StackOverflowError were a runtime exception.
- D.The code would print caught if StackOverflowError were a checked exception.
- E.The code will print caught if question() throws exception NullPointer-Exception .
- Answer & Explanation
- Report
Answer : [A, C, D]
Explanation :
Explanation :
Option (a) is correct. The control will be transferred to the exception
handler for StackOverflowError when it’s encountered. Hence it will print caught . Options (c) and (d) are correct. Exception handlers execute when the corresponding checked or runtime exceptions are thrown. Option (e) is incorrect. An exception handler for class StackOverflow can’t handle exceptions of class NullPointerException because NullPointerException is not a superclass of StackOverflowError . |
40.
A class Student is defined as follows:
public class Student {
private String fName;
private String lName;
public Student(String first, String last) {
fName = first; lName = last;
}
public String getName() { return fName + lName; }
}
The creator of the class later changes the method getName as follows:
public String getName() {
return fName + " " + lName;
}
What are the implications of this change? (Select 2 options.)
public class Student {
private String fName;
private String lName;
public Student(String first, String last) {
fName = first; lName = last;
}
public String getName() { return fName + lName; }
}
The creator of the class later changes the method getName as follows:
public String getName() {
return fName + " " + lName;
}
What are the implications of this change? (Select 2 options.)
- A.The classes that were using the class Student will fail to compile.
- B.The classes that were using the class Student will work without any compilation issues.
- C.The class Student is an example of a well-encapsulated class.
- D.The class Student exposes its instance variable outside the class.
- Answer & Explanation
- Report
Answer : [B, C]
Explanation :
Explanation :
This is an example of a well-encapsulated class. There is no change in the method signature of method getName after it’s modified. Hence, none of the code that uses this class and method will face any compilation issues. Its instance variables ( fName and lName ) aren’t exposed outside the class. They are available only via a public method: getName . |