Q1. Given:
What is the result?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: D
Q2. Given the code fragment:
What is the result?
A. Sum is 600
B. Compilation fails at line n1.
C. Compilation fails at line n2.
D. A ClassCastException is thrown at line n1.
E. A ClassCastException is thrown at line n2.
Answer: C
Q3. Given the following two classes:
How should you write methods in the ElectricAccount class at line n1 so that the member variable bill is always equal to the value of the member variable kwh multiplied by the member variable rate?
Any amount of electricity used by a customer (represented by an instance of the customer class) must contribute to the customer's bill (represented by the member variable bill) through the method useElectricity method. An instance of the customer class should never be able to tamper with or decrease the value of the member variable bill.
A. Option A
B. Option B
C. Option C
D. Option D
Answer: B
Q4. Given:
What is the result?
A. Initialized Started
B. Initialized Started Initialized
C. Compilation fails
D. An exception is thrown at runtime
Answer: B
Q5. Given the code fragment:
And given the requirements:
. If the value of the qty variable is greater than or equal to 90, discount = 0.5
. If the value of the qty variable is between 80 and 90, discount = 0.2
Which two code fragments can be independently placed at line n1 to meet the requirements?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Answer: A,C
Q6. Given:
public class String1 {
public static void main(String[] args) {
String s = "123";
if (s.length() >2)
s.concat("456");
for(int x = 0; x <3; x++)
s += "x";
System.out.println(s);
}
}
What is the result?
A. 123
B. 123xxx
C. 123456
D. 123456xxx
E. Compilation fails
Answer: B
Explanation: 123xxx
The if clause is not applied.
Note: Syntax of if-statement:
if ( Statement ) {
}
Q7. Given:
Which inserted at line 11, will provide the following output?
[21, 15, 11]
A. list.removelf(e > e%2 != 0);
B. list.removelf(e -> e%2 != 0);
C. Ust.removelf(e -> e%2 = 0);
D. list.remove(e -> e%2 = 0);
E. None of the above.
Answer: C
Explanation:
In output we can see that only odd numbers present, so we need to remove only even numbers to get expected output. From Java SE 8, there is new method call removelf which takes predicate object and remove elements which satisfies predicate condition. Predicate has functional method call take object and check if the given condition met or not, if met it returns true, otherwise false. Option C we have passed correct lambda expression to check whether the number is odd or even that matches to the functional method of predicate interface. Option A is incorrect as it is invalid lambda expression. Option B is incorrect as it removes all odd numbers. Option D is incorrect as there is no remove method that takes predicate as argument. https://docs.oracle.eom/javase/8/docs/api/java/util/ArrayList.html
Q8. Given:
public class MyClass {
public static void main(String[] args) {
String s = " Java Duke ";
int len = s.trim().length();
System.out.print(len);
}
}
What is the result?
A. 8
B. 9
C. 11
D. 10
E. Compilation fails
Answer: B
Explanation: Java - String trim() Method
This method returns a copy of the string, with leading and trailing whitespace omitted.
Q9. Given the code fragment:
Which two modifications, made independently, enable the code to compile?
A. Make the method at line n1 public.
B. Make the method at line n2 public.
C. Make the method at line n3 public.
D. Make the method at line n3 protected.
E. Make the method at line n4 public.
Answer: C,D
Q10. Given the following code:
What are the values of each element in intArr after this code has executed?
A. 15, 60, 45, 90, 75
B. 15, 90, 45, 90, 75
C. 15, 30, 75, 60, 90
D. 15, 30, 90, 60, 90
E. 15, 4, 45, 60, 90
Answer: C
Q11. Given:
public class App { // Insert code here System.out.print("Welcome to the world of Java"); } }
Which two code fragments, when inserted independently at line // Insert code here, enable the program to execute and print the welcome message on the screen?
A. static public void main (String [] args) {
B. static void main (String [] args) {
C. public static void Main (String [] args) {
D. public static void main (String [] args) {
E. public void main (String [] args) {
Answer: A,D
Explanation:
Incorrect:
Not B: No main class found.
Not C: Main method not found
not E: Main method is not static.
Q12. Given the code fragment:
StringBuilder sb = new StringBuilder ( ) ;
Sb.append (“world”);
Which code fragment prints Hello World?
A. sb.insert(0,"Hello ");
System.out.println(sb);
B. sb.append(0,"Hello ");
System.out.println(sb);
C. sb.add(0,"Hello ");
System.out.println(sb);
D. sb.set(0,"Hello ");
System.out.println(sb);D
Answer: A
Explanation: The java.lang.StringBuilder.insert(int offset, char c) method inserts the string representation of the char argument into this sequence. The second argument is inserted into the contents of this sequence at the position indicated by offset. The length of this sequence increases by one.The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.
Reference: Java.lang.StringBuilder.insert() Method
Q13. Given the code fragment:
What is the result?
A. 20
B. 25
C. 29
D. Compilation fails
E. AnArrayIndexOutOfBoundsException is thrown at runtime
Answer: A
Q14. What is the name of the Java concept that uses access modifiers to protect variables and hide them within a class?
A. Encapsulation
B. Inheritance
C. Abstraction
D. Instantiation
E. Polymorphism
Answer: A
Q15. Given the code fragment:
class Student {
int rollnumber;
String name;
List cources = new ArrayList();
// insert code here
public String toString() {
return rollnumber + " : " + name + " : " + cources;
}
}
And,
public class Test {
public static void main(String[] args) {
List cs = newArrayList();
cs.add("Java");
cs.add("C");
Student s = new Student(123,"Fred", cs);
System.out.println(s);
}
}
Which code fragment, when inserted at line // insert code here, enables class Test to print 123 : Fred : [Java, C]?
A.
private Student(int i, String name, List cs) {
/* initialization code goes here */
}
B.
public void Student(int i, String name, List cs) {
/* initialization code goes here */
}
C.
Student(int i, String name, List cs) {
/* initialization code goes here */
}
D.
Student(int i, String name, ArrayList cs) {
/* initialization code goes here */
}
Answer: C
Explanation:
Incorrect:
Not A: Student has private access line: Student s = new Student(123,"Fred", cs);
Not D: Cannot be applied to given types. Line: Student s = new Student(123,"Fred", cs);