- Home
- Programming
- Java SE7
46.
What is the output of the following code? (Select 1 option.)
class Book {
String ISBN;
Book(String val) {
ISBN = val;
}
}
class TestEquals {
public static void main(String... args) {
Book b1 = new Book("1234-4657");
Book b2 = new Book("1234-4657");
System.out.print(b1.equals(b2) +":");
System.out.print(b1 == b2);
}
}
class Book {
String ISBN;
Book(String val) {
ISBN = val;
}
}
class TestEquals {
public static void main(String... args) {
Book b1 = new Book("1234-4657");
Book b2 = new Book("1234-4657");
System.out.print(b1.equals(b2) +":");
System.out.print(b1 == b2);
}
}
- A.true:false
- B.true:true
- C.false:true
- D.false:false
- E.Compilation error—there is no equals method in the class Book .
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
The comparison operator determines whether the reference variables
refer to the same object. Because the reference variables b1 and b2 refer to different
objects, b1==b2 prints false . The method equals is a public method defined in the class java.lang.Object . Because the class Object is the superclass for all the classes in Java, the method equals is inherited by all classes. Hence, the code compiles successfully. The default implementa- tion of method equals in the base class compares the object references and returns true if both the reference variables refer to the same object and false otherwise. Because the class Book doesn’t override this method, the method equals in the base class Object is called for b1.equals(b2) , which returns false . Hence, the code prints: false:false |
47.
Which of the following statements are correct? (Select 2 options.)
- A.StringBuilder sb1 = new StringBuilder() will create a StringBuilder object with no characters, but with an initial capacity to store 16 chars.
- B.StringBuilder sb1 = new StringBuilder(5*10) will create a StringBuilder object with a value 50 .
- C.Unlike the class String , the concat method in StringBuilder modifies the value of a StringBuilder object.
- D.The insert method can be used to insert a character, number, or String at the start or end or a specified position of a StringBuilder .
- Answer & Explanation
- Report
Answer : [A, D]
Explanation :
Explanation :
There is no concat method in the StringBuilder class. It defines a whole
army of append methods (overloaded methods) to add data at the end to a String-
Builder object. new StringBuilder(50) creates a StringBuilder object with no characters, but with an initial capacity to store 50 chars. |
48.
Given the following definition of the class Animal and the interface Jump ,
select the correct array declarations and initialization (choose 3 options):
interface Jump {}
class Animal implements Jump {}
interface Jump {}
class Animal implements Jump {}
- A.Jump eJump1[] = {null, new Animal()};
- B.Jump[] eJump2 = new Animal()[22];
- C.Jump[] eJump3 = new Jump[10];
- D.Jump[] eJump4 = new Animal[87];
- E.Jump[] eJump5 = new Jump()[12];
- Answer & Explanation
- Report
Answer : [A, C, D]
Explanation :
Explanation :
Option (b) is incorrect because the right side of the expression is trying to create a single object of the class Animal by using round brackets, () . At the same time, it’s also using the square brackets, [] , to define an array. This combination is invalid. Option (e) is incorrect. Apart from using an invalid syntax to initialize an array (as mentioned previously), it also tries to create objects of the interface Jump . Objects of interfaces can’t be created. |
49.
What is the output of the following code? (Select 1 option.)
import java.util.*;
class EJGArrayL {
public static void main(String args[]) {
ArrayList seasons = new ArrayList<>();
seasons.add(1, "Spring"); seasons.add(2, "Summer");
seasons.add(3, "Autumn"); seasons.add(4, "Winter");
seasons.remove(2);
for (String s : seasons)
System.out.print(s + ", ");
}
}
import java.util.*;
class EJGArrayL {
public static void main(String args[]) {
ArrayList
seasons.add(1, "Spring"); seasons.add(2, "Summer");
seasons.add(3, "Autumn"); seasons.add(4, "Winter");
seasons.remove(2);
for (String s : seasons)
System.out.print(s + ", ");
}
}
- A.Spring, Summer, Winter,
- B.Spring, Autumn, Winter,
- C.Autumn, Winter,
- D.Compilation error
- E.Runtime exception
- Answer & Explanation
- Report
Answer : [E]
Explanation :
Explanation :
The code throws a runtime exception, IndexOutOfBoundsException , because the ArrayList is trying to insert its first element at position 0 . Before the first call to the method add , the size of the ArrayList seasons is 0 . Because season ’s first element is stored at position 0 , a call to store its first element at position 1 will throw a RuntimeException . The elements of an ArrayList can’t be added to a higher position if lower positions are available. |
50.
What is the output of the following code? (Select 1 option.)
class EIf {
public static void main(String args[]) {
bool boolean = false;
if (boolean = true)
System.out.println("true");
else
System.out.println("false");
}
}
class EIf {
public static void main(String args[]) {
bool boolean = false;
if (boolean = true)
System.out.println("true");
else
System.out.println("false");
}
}
- A.The class will print true .
- B.The class will print false .
- C.The class will print true if the if condition is changed to boolean == true .
- D.The class will print false if the if condition is changed to boolean != true .
- E.The class won’t compile.
- Answer & Explanation
- Report
Answer : [E]
Explanation :
Explanation :
This question tries to trick you on two points. First, there is no data type bool in Java. Second, the name of an identifier can’t be the same as a reserved word. The code tries to define an identifier of type bool with the name boolean . |