- Home
- Programming
- Java SE7
26.
Assume that Oracle has asked you to create a method that returns the con-
catenated value of two String objects. Which of the following methods do you think
can accomplish this job? (Select 2 options.)
- A.public String add(String 1, String 2) {
return str1 + str2;
} - B.private String add(String s1, String s2) {
return s1.concat(s2);
} - C.protected String add(String value1, String value2) {
return value2.append(value2);
} - D.String subtract(String first, String second) {
return first.concat(second.substring(0));
}
- Answer & Explanation
- Report
Answer : [B, D]
Explanation :
Explanation :
Option (a) is incorrect. This method defines method parameters with
invalid identifier names. Identifiers can’t start with a digit. Option (b) is correct. The method requirements don’t talk about the access modifier of the required method. It can have any accessibility. Option (c) is incorrect because the class String doesn’t define any append method. Option (d) is correct. Even though the name of the method— subtract —isn’t an appropriate name for a method that tries to concatenate two values, it does accom- plish the required job. |
27.
In Java, the class String is defined in the package java.lang , and this pack-
age is automatically imported in all the Java classes. Given this statement, which of the
following options represents the correct definition of class EJava , which can define a
variable of class String ? (Choose 2 options.)
- A.import java.lang;
class EJava {
String guru;
} - B.import java.lang.String.*;
class EJava {
String guru;
} - C.class EJava {
String guru;
} - D.import java.lang.String;
import java.lang.String;
class EJava {
String guru;
}
- Answer & Explanation
- Report
Answer : [C,D]
Explanation :
Explanation :
Options (a) and (b) are incorrect. The code in both these options won’t
compile because they use incorrect import statement. The following line of code will
import all the classes from package java.lang (including class String ): import java.lang.*; You can use the following import statement to import just the class String : import java.lang.String; Option (c) is correct. The class EJava can create variables of the class String because the class java.lang.String is automatically imported in all the Java classes. Hence, it’s available to EJava , even if this class doesn’t import it explicitly. Option (d) is correct. It doesn’t make a difference if you import the same class more than once in your code. |
28.
Given the following definitions of the class ChemistryBook , select the statements that are correct individually (choose 2 options):
import java.util.ArrayList;
class ChemistryBook {
public void read() {}
public String read() { return null; }
ArrayList read(int a) { return null; }
}
import java.util.ArrayList;
class ChemistryBook {
public void read() {}
public String read() { return null; }
ArrayList read(int a) { return null; }
}
- A.Methods marked with //METHOD1 and //METHOD2 are correctly overloaded methods
- B.Methods marked with //METHOD2 and //METHOD3 are correctly overloaded methods.
- C.Methods marked with //METHOD1 and //METHOD3 are correctly overloaded methods.
- D.All the methods—methods marked with //METHOD1 , //METHOD2 , and //METHOD3 — are correctly overloaded methods.
- Answer & Explanation
- Report
Answer : [B, C]
Explanation :
Explanation :
Options (a) and (d) are incorrect because the methods read marked
with //METHOD1 and //METHOD2 only differ in their return types, void and String . Overloaded methods can’t be defined with only a change in their return types; hence, these methods don’t qualify as correctly overloaded methods. Note that the presence of methods marked with //METHOD1 and //METHOD2 together will cause a compilation error. |
29.
Given the following definition of the class Home , select the correct statements
(choose 4 options):
class Home {
String name
int rooms;
Home() {}
}
class Home {
String name
int rooms;
Home() {}
}
- A.The class Home will be provided a default constructor.
- B.The class Home won’t be provided a default constructor.
- C.A default constructor can’t coexist with overloaded constructors.
- D.A default constructor doesn’t accept any method parameters.
- E.After compilation, the class Home has only a no-argument constructor.
- F.After compilation, the class Home has two constructors: a no-argument con- structor and a default constructor.
- G.When an object of class Home is created, its variables name and rooms are not assigned any default values.
- Answer & Explanation
- Report
Answer : [B, C, D, E]
Explanation :
Explanation :
Option (b) is correct. The class Home doesn’t contain a default construc-
tor. A default constructor is generated by Java when the user doesn’t define any con-
structor. In this case, the class Home does define a constructor. Option (c) is correct. A default constructor is generated only in the absence of a constructor. Hence, it can’t coexist with other constructors. Option (d) is correct. The default constructor doesn’t accept any method parame- ters. It initializes the instance variables of a class to their default values. Option (e) is correct and (f) is incorrect. No default constructor will be generated for class Home because Home already defines a no-argument constructor. A no-argument constructor is a constructor that defines no method parameters. After compilation, class Home has only one constructor that doesn’t accept any method parameters. Option (g) is incorrect. If you don’t assign explicit values to instance variables of a class, they are initialized to their default values. |
30.
Given the following code, which option, if used to replace /* INSERT CODE HERE */ , will make the code print numbers that are completely divisible by 14? (Select 1 option.)
for (int ctr = 2; ctr <= 30; ++ctr) {
if (ctr % 7 != 0)
//INSERT CODE HERE
if (ctr % 14 == 0)
System.out.println(ctr);
}
for (int ctr = 2; ctr <= 30; ++ctr) {
if (ctr % 7 != 0)
//INSERT CODE HERE
if (ctr % 14 == 0)
System.out.println(ctr);
}
- A.continue;
- B.Exit
- C.break
- D.End
- Answer & Explanation
- Report
Answer : [A]
Explanation :
Explanation :
Options (b) and (d) are incorrect because exit and end aren’t valid
statements in Java. Option (c) is incorrect. Using break will terminate the for loop during the first iteration of the for loop so that no output is printed. |