Q1. Given the code fragment:
What could expression1 and expression2 be, respectively, in order to produce output –8, 16?
A. + +a, - -b
B. + +a, b- -
C. A+ +, - - b
D. A + +, b - -
Answer: D
Q2. Which usage represents a valid way of compiling java source file with the name "Main"?
A. javac Main.java
B. java Main.class
C. java Main.java
D. javac Main
E. java Main
Answer: A
Explanation: The compiler is invoked by the javac command. When compiling a Java class, you must include the file name, which houses the main classes including the Java extension. So to run Main.java file we have to use command in option A. TO execute Java program we can use Java command but can't use it for compiling. https://docs.oracle.com/javase/tutorial/getStarted/application/index.html
Q3. Given the code fragment:
What is the result?
A. 10 : 10
B. 5 : 5
C. 5 : 10
D. Compilation fails
Answer: A
Q4. Given:
public class Equal {
public static void main(String[] args) {
String str1 = "Java";
String[] str2 = {"J","a","v","a"};
String str3 = "";
for (String str : str2) {
str3 = str3+str;
}
boolean b1 = (str1 == str3);
boolean b2 = (str1.equals(str3));
System.out.print(b1+", "+b2);
}
What is the result?
A. true, false
B. false, true
C. true, true
D. false, false
Answer: B
Explanation: == strict equality. equals compare state, not identity.
Q5. Given the code fragment:
Assume that the system date is June 20, 2014. What is the result?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: A
Q6. Given the code fragment from three files:
Which code fragment, when inserted at line 2, enables the code to compile?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Answer: E
Q7. Which of the following can fill in the blank in this code to make it compile? (Select 2 options.)
A. On line 1, fill in throws
B. On line 1, fill in throws new
C. On line 2, fill in throw new
D. On line 2, fill in throws
E. On line 2, fill in throws new
Answer: A,C
Explanation:
Option A and C are the correct answer.
In a method declaration, the keyword throws is used. So here at line 1 we have to use
option A.
To actually throw an exception, the keyword throw is used and a new exception is created,
so at line 2 we have to use throw and new keywords, which is option C. Finally it will look
like;
public void method() throws Exception {
throw new Exception0;
}
REFERENCE : httpsy/docs.oracle.com/javase/tutorial/essential/io/fileOps.html#exception
The correct answer is: On line 1, fill in throws. On line 2, fill in throw new
Q8. Given:
A. ns = 50 S = 125 ns = 125 S = 125 ns = 100 S = 125
B. ns = 50 S = 125 ns = 125 S = 125 ns = 0 S = 125
C. ns = 50 S = 50 ns = 125 S = 125 ns = 100 S = 100
D. ns = 50 S = 50 ns = 125 S = 125 ns = 0 S = 125
Answer: B
Q9. Given:
What is the result?
A. myStr: 9009, myNum: 9009
B. myStr: 7007, myNum: 7007
C. myStr: 7007, myNum: 9009
D. Compilation fails
Answer: C
Q10. Given:
class Base {
// insert code here
}
public class Derived extends Base{
public static void main(String[] args) {
Derived obj = new Derived();
obj.setNum(3);
System.out.println("Square = " + obj.getNum() * obj.getNum());
}
}
Which two options, when inserted independently inside class Base, ensure that the class is being properly encapsulated and allow the program to execute and print the square of the number?
A. private int num; public int getNum() { return num; }public void setNum(int num) { this.num = num;}
B. public int num; protected public int getNum() { return num; }protected public void setNum(int num) { this.num = num;}
C. private int num;public int getNum() {return num;} private void setNum(int num) { this.num = num;}
D. protected int num; public int getNum() { return num; } public void setNum(int num) { this.num = num;}
E. protected int num; private int getNum() { return num; } public void setNum(int num) { this.num = num;}
Answer: A,D
Explanation:
Incorrect:
Not B: illegal combination of modifiers: protected and public
not C: setNum method cannot be private.
not E: getNum method cannot be private.
Q11. Given:
What is the result?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: B
Q12. Given the code fragment:
public class ForTest {
public static void main(String[] args) {
int[] array = {1, 2, 3};
for ( foo ) {
}
}
Which three code fragments, when replaced individually for foo, enables the program to compile?
A. int i : array
B. int i = 0; i < 1;
C. ; ;
D. ; i < 1; i++
E. i = 0; i<1;
Answer: A,B,C
Q13. Given:
class Mid {
public int findMid(int n1, int n2) {
return (n1 + n2) / 2;
}
}
public class Calc extends Mid {
public static void main(String[] args) {
int n1 = 22, n2 = 2;
// insert code here
System.out.print(n3);
}
}
Which two code fragments, when inserted at // insert code here, enable the code to compile and print 12?
A. Calc c = new Calc(); int n3 = c.findMid(n1,n2);
B. int n3 = super.findMid(n1,n3);
C. Calc c = new Mid(); int n3 = c.findMid(n1, n2);
D. Mid m1 = new Calc(); int n3 = m1.findMid(n1, n2);
E. int n3 = Calc.findMid(n1, n2);
Answer: A,D
Explanation:
Incorrect:
Not B: circular definition of n3.
Not C: Compilation error. line Calc c = new Mid();
required: Calc
found: Mid
Not E: Compilation error. line int n3 = Calc.findMid(n1, n2);
non-static method findMid(int,int) cannot be referenced from a static context
Q14. Given:
What is the result?
A. int main 1
B. Object main 1
C. String main 1
D. Compilation fails
E. An exception is thrown at runtime
Answer: C
Q15. Given:
What is the result?
A. True false
B. True null
C. Compilation fails
D. A NullPointerException is thrown at runtime
Answer: A