- Home
- Programming
- Java SE7
16.
Which statements describe the use of exception handling in Java? (Select 2 options.)
- A.Exception handling can prevent an application from crashing or producing incorrect outputs or incorrect input values.
- B.Exception handlers can’t define an alternative flow of action in the event of an exception.
- C.Exception handlers enable a programmer to define separate code blocks for handling different types of exceptions.
- D.Exception handlers help with efficient inheritance.
- E.Exception handlers help with efficient inheritance.
- Answer & Explanation
- Report
Answer : [A, C]
Explanation :
Explanation :
Option (b) is incorrect. One of the main purposes of an exception handler is to define an alternative flow of action.
Options (d) and (e) are incorrect. Definitions of exception handlers, well-encapsulated classes, and efficient inheritance aren’t related in behavior as stated by these options. |
17.
Which statements are true for reference and primitive variables? (Select 3 options.)
- A.The names of the reference variables are limited to a length of 256 characters.
- B.There is no limit on the length of the names of primitive variables.
- C.Multiple reference variables may refer to exactly the same object in memory.
- D.Values stored by primitive and reference variables can be compared for equal- ity by using the equals operator ( == ) or by using the method equals .
- E.A primitive variable can’t refer to an object and vice versa.
- Answer & Explanation
- Report
Answer : [B, C, E]
Explanation :
Explanation :
Option (a) is incorrect. Theoretically, there is no limit on the number of
characters that can be used to define the name of a primitive variable or object reference. Option (d) is incorrect. Unlike object references, primitive variables can be compared for equality by using the equals operator ( == ) only. Option (e) is correct. A primitive variable can never refer to an object and vice versa. |
18.
What is the output of the following code? (Select 1 option.)
public class Handset {
public static void main(String... args) {
double price;
String model;
System.out.println(model + price);
}
}
public class Handset {
public static void main(String... args) {
double price;
String model;
System.out.println(model + price);
}
}
- A.null0
- B.null0.0
- C.0
- D.0.0
- E.Compilation error
- Answer & Explanation
- Report
Answer : [E]
Explanation :
Explanation :
The local variables (variables that are declared within a method) aren’t initialized with their default values. If you try to print the value of a local variable before initializing it, the code won’t compile. |
19.
What is the output of the following code? (Select 1 option.)
public class Sales {
public static void main(String args[]) {
int salesPhone = 1;
System.out.println(salesPhone++ + ++salesPhone +
++salesPhone);
}
}
public class Sales {
public static void main(String args[]) {
int salesPhone = 1;
System.out.println(salesPhone++ + ++salesPhone +
++salesPhone);
}
}
- A.5
- B.6
- C.8
- D.9
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
Understanding the following rules will enable you to answer this question correctly: --> An arithmetic expression is evaluated from left to right. --> When an expression uses the unary increment operator ( ++ ) in postfix notation, its value increments just after its original value is used in an expression. --> When an expression uses the unary increment operator ( ++ ) in prefix notation, its value increments just before its value is used in an expression. The initial value of the variable salesPhone is 1 . Let’s evaluate the result of the arith- metic expression salesPhone++ + ++salesPhone + ++salesPhone step by step: 1) The first occurrence of salesPhone uses ++ in postfix notation, so its value is used in the expression before it is incremented by 1 . This means that the expres 1 + ++salesPhone + ++salesPhone 2) Note that the previous usage of ++ in postfix increments has already incremented the value of salesPhone to 2 . The second occurrence of salesPhone uses ++ in prefix notation, so its value is used in the expression after it is incremented by 1 , to 3 . This means that the expression evaluates to 1 + 3 + ++salesPhone 3) The third occurrence of salesPhone again uses ++ in prefix notation, so its value is used in the expression after it is incremented by 1 , to 4 . This means that the expression evaluates to 1 + 3 + 4 |
20.
Which of the following options defines the correct structure of a Java class?
(Select 1 option.)
- A.package com.ejava.guru;
package com.ejava.oracle;
class MyClass { } - B.import com.ejava.guru.*;
import com.ejava.oracle.*;
package com.ejava;
class MyClass { } - C.class MyClass {
import com.ejava.guru.*;
} - D.class MyClass {
int abc;
}
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
Option (a) is incorrect. A class can’t define more than one package
statement. Option (b) is incorrect. Though a class can import multiple packages in a class, the package statement must be placed before the import statement. Option (c) is incorrect. A class can’t define an import statement within its class body. The import statement appears before the class body. Option (d) is correct. In the absence of any package information, this class becomes part of the default package. |