- Home
- Programming
- Java SE7
11.
Which of the following statements are true? (Select 2 options.)
- A.Given that you changed the access of a public method B , in class A , to a pri- vate method, class C that uses method B will fail to compile.
- B.Given that you changed the access of a private method B , in class A , to a pub- lic method, none of the classes that use class A will fail to compile.
- C.Given that you changed the access of a protected method B , in class A , to a method with default access, class C from the same package as class A won’t be able to access method B .
- D.A change in the accessibility of the methods in your class never affects any other class that uses your class.
- Answer & Explanation
- Report
Answer : [A,B]
Explanation :
Explanation :
Option (a) is correct. A public method is accessible to other classes. If
you change the accessibility of a public method to a private method, it will no lon-
ger be accessible outside its class. Any class that uses such a method will fail to compile
after this modification. Option (b) is correct. A private method isn’t accessible outside the class in which it’s defined. In other words, a private method isn’t known to the other classes, so it can’t be accessed by other classes. If classes can’t even access the private methods of other classes, it won’t make a difference to them if their accessibility is changed. Option (c) is incorrect. A method with default access can be accessed by classes defined in the same package. Option (d) is incorrect. A change in the accessibility of the methods in your class affects other classes that use your class. If you assign a weaker accessibility to any of your methods, it may no longer be accessible to the other classes. If this happens, the other class will fail to compile. |
12.
Which of the following statements are correct? (Select 3 options)
- A.You may not be able to handle all the checked exceptions in your code.
- B.If required, you can handle all the runtime exceptions in your code.
- C.You can handle an exception in your code even if you don’t know its exact name.
- D.A single exception handler can be used to handle all types of runtime and checked exceptions.
- E.You must handle all errors that can be thrown by your code
- F.Runtime exceptions are also known as checked exceptions.
- Answer & Explanation
- Report
Answer : [B, C, D]
Explanation :
Explanation :
Option (a) is incorrect. If, for example, callingMethod() calls called-
Method() , which throws checked exceptions, callingMethod() can’t ignore checked
exceptions thrown by calledMethod() . callingMethod() should handle these exceptions itself or declare them to be thrown. callingMethod() can’t ignore any checked exceptions thrown by calledMethod() . If it tries to do so, the code won’t compile. Option (b) is correct. It is indeed possible to handle all the runtime exceptions in your code. Options (c) and (d) are correct. The superclass of all types of exceptions (checked and runtime) is class java.lang.Exception , so if you define a handler for java.lang .Exception in your code, you are able to handle all runtime and checked exceptions, and this will include any exceptions whose names you don’t know. Option (e) is incorrect. Even though errors can be caught by an exception handler, you shouldn’t handle them because they’re serious exceptions thrown by the JVM as a result of an error in the environment state that processes your code. Option (f) is incorrect because runtime exceptions are also known as unchecked exceptions. |
13.
Given the following code,
class MainMethod {
public static void main(String... args) {
System.out.println(args[0]+":"+ args[2]);
}
}
what’s its output if it’s executed using the following command? (Select 1 option.)
java MainMethod 1+2 2*3 4-3 5+1
class MainMethod {
public static void main(String... args) {
System.out.println(args[0]+":"+ args[2]);
}
}
what’s its output if it’s executed using the following command? (Select 1 option.)
java MainMethod 1+2 2*3 4-3 5+1
- A.java:1+2
- B.java:3
- C.MainMethod:2*3
- D.MainMethod:6
- E.1+2:2*3
- F.3:3
- G.6
- H.1+2:4-3
- I.31
- J.4
- Answer & Explanation
- Report
Answer : [H]
Explanation :
Explanation :
This question tests you on multiple points: --> The arguments that are passed on to the main method—The keyword java and the name of the class ( MainMethod ) aren’t passed as arguments to the main method. The arguments following the class name are passed to the main method. In this case, four method arguments are passed to the main method, as follows: args[0]: 1+2 args[1]: 2*3 args[2]: 4-3 args[3]: 5+1 --> The type of the arguments that are passed to the main method—The main method accepts arguments of type String . All the numeric expressions— 1+2 , 2*3 , 5+1 and 4-3 —are passed as literal String values. These won’t be evaluated when you try to print their values. Hence, args[0] won’t be printed as 3 . It will be printed as 1+2 . --> + operations with String array elements—Because the array passed to the main method contains all the String values, using the + operand with its individual values will concatenate its values. It won’t add the values, if they are numeric expressions. Hence, "1+2"+"4-3" won’t evaluate to 31 or 4 . |
14.
What is the output of the following code? (Select 1 option.)
class Person {
int age;
float height;
boolean result;
String name;
}
public class EJava {
public static void main(String arguments[]) {
Person person = new Person();
System.out.println(person.name + person.height + person.result
+ person.age);
}
}
class Person {
int age;
float height;
boolean result;
String name;
}
public class EJava {
public static void main(String arguments[]) {
Person person = new Person();
System.out.println(person.name + person.height + person.result
+ person.age);
}
}
- A.null0.0false0
- B.null0false0
- C.null0.0ffalse0
- D.0.0false0
- E.0false0
- F.0.0ffalse0
- G.null0.0true0
- H.0true0
- I.0.0ftrue0
- Answer & Explanation
- Report
Answer : [A]
Explanation :
Explanation :
The instance variables of a class are all assigned default values if no
explicit value is assigned to them. Here are the default values of the primitive data
types and the objects: char -> \u0000 byte, short, int -> 0 long -> 0L float-> 0.0f double -> 0.0d boolean -> false objects -> null |
15.
Given the following code, which option, if used to replace /* INSERT CODE HERE
*/ , will make the code print the value of the variable pagesPerMin ? (Select 1 option.)
class Printer {
int inkLevel;
}
class LaserPrinter extends Printer {
int pagesPerMin;
public static void main(String args[]) {
Printer myPrinter = new LaserPrinter();
System.out.println(/* INSERT CODE HERE */);
}
}
class Printer {
int inkLevel;
}
class LaserPrinter extends Printer {
int pagesPerMin;
public static void main(String args[]) {
Printer myPrinter = new LaserPrinter();
System.out.println(/* INSERT CODE HERE */);
}
}
- A.( LaserPrinter)myPrinter.pagesPerMin
- B.myPrinter.pagesPerMin
- C.LaserPrinter.myPrinter.pagesPerMin
- D.((LaserPrinter)myPrinter).pagesPerMin
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
Option (a) is incorrect because (LaserPrinter) tries to cast myPrinter
.pagesPerMin to LaserPrinter , which is incorrect. This code won’t compile. Option (b) is incorrect. The type of reference variable myPrinter is Printer . myPrinter refers to an object of the class LaserPrinter , which extends the class Printer . A reference variable of the base class can’t access the variables and methods defined in its subclass without an explicit cast. Option (c) is incorrect. LaserPrinter.myPrinter treats LaserPrinter as a variable, although no variable with this name exists in the question’s code. This code fails to compile. |