mock exam Flashcards

from programmer 1 certification guide

1
Q
Given the following definition of the classes Animal, Lion, and Jumpable,
select  the  correct  combinations  of  assignments  of  a  variable  that  don’t  result  in
compilation errors or runtime exceptions (select 2 options).
interface Jumpable {}
class Animal {}
class Lion extends Animal implements Jumpable {}
a Jumpable var1 = new Jumpable();
b Animal var2 = new Animal();
c Lion var3 = new Animal();
d Jumpable var4 = new Animal();
e Jumpable var5 = new Lion();
f Jumpable var6 = (Jumpable) (new Animal( ) );
A

Answer: b, e
Explanation: Option (a) is incorrect. An interface can’t be instantiated.
Option (c) is incorrect. A reference variable of a derived class can’t be used to
refer to an object of its base class.
Option (d) is incorrect. A reference variable of type Jumpable can’t be used to
refer to an object of the class Animal because Animal doesn’t implement the interface
Jumpable.
Option (f) is incorrect. Although this line of code will compile successfully, it will
throw a ClassCastException at runtime. You can explicitly cast any object to an inter-face, even if it doesn’t implement it to make the code compile. But if the object’s class
doesn’t implement the interface, the code will throw a ClassCastException at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Given the following code, which option, if used to replace /* INSERT CODE
HERE /, will make the code print 1? (Select 1 option.)
try {
String[][] names = {{“Andre”, “Mike”}, null, {“Pedro”}};
System.out.println (names[2][1].substring(0, 2));
} catch (/
INSERT CODE HERE*/) {
System.out.println(1);
}
a IndexPositionException e
b NullPointerException e
c ArrayIndexOutOfBoundsException e
d ArrayOutOfBoundsException e

A

Answer: c
Explanation: Options (a) and (d) are incorrect because the Java API doesn’t define
any exception classes with these names.
[8.2] Create a try-catch block and determine how exceptions alter normal
program flow
Here’s a list of the array values that are initialized by the code in this question:
names[0][0] = “Andre”
names[0][1] = “Mike”
names[1] = null
names[2][0] = “Pedro”
Because the array position [2][1] isn’t defined, any attempt to access it will throw an
ArrayIndexOutOfBoundsException.
An attempt to access any position of the second array—that is, names[1][0]—will
throw a NullPointerException because names[1] is set to null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
What is the output of the following code? (Select 1 option.)
public static void main(String[] args) {
int a = 10; String name = null;
    try {
        a = name.length();                   //line1
        a++;                                 //line2
    } catch (NullPointerException e){
        \++a;                               
return;
    } catch (RuntimeException e){
        a--;
return;
    } finally {
System.out.println(a);
    }
}
a 5
b 6
c 10
d 11
e 12
f Compilation error
g No output
h Runtime exception
A
Answer: d
Explanation:  Because  the  variable name  isn’t  assigned  a  value,  you  can’t  call  an
instance method (length()) using it. The following line of code will throw a Null-PointerException:
name.length();
When  an  exception  is  thrown,  the  control  is  transferred  to  the  exception  handler,
skipping the execution of the remaining lines of code in the try block. So the code
(a++) doesn’t execute at the comment marked with line2. 
  The  code  defines  an  exception  handler  for  both NullPointerException  and
RuntimeException. When an exception is thrown, more than one exception handler
won’t execute. In this case, the exception handler for NullPointerException will exe-cute because it’s more specific and it’s defined earlier than RuntimeException. The
exception handler for NullPointerException includes the following code:
\++a;
return;
The preceding code increments the value of the variable a by 1; and before it exits the
method main, due to the call to the statement return, it executes the finally block,
outputting the value 11. A finally block executes even if the catch block includes a
return statement.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Given the following class definition,
class Student { int marks = 10; }
what is the output of the following code? (Select 1 option.)
class Result {
    public static void main(String... args) {
Student s = new Student();
switch (s.marks) {
            default: System.out.println("100");
            case 10: System.out.println("10");
            case 98: System.out.println("98");
        }
    }
}
a 100
10
98
b 10
98
c 100
d 10
A

Answer: b
Explanation: The default case executes only if no matching values are found. In this
case, a matching value of 10 is found and the case label prints 10. Because a break statement doesn’t terminate this case label, the code execution continues and exe-cutes the remaining statements within the switch block, until a break statement ter-minates it or it ends

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
Given the following code, which code can be used to create and initialize an
object of the class ColorPencil? (Select 2 options.)
class Pencil {}
class ColorPencil extends Pencil {
String color;
    ColorPencil(String color) {this.color = color;}
}
a ColorPencil var1 = new ColorPencil();
b ColorPencil var2 = new ColorPencil(RED);
c ColorPencil var3 = new ColorPencil("RED");
d Pencil var4 = new ColorPencil("BLUE");
A

Answer: c, d
Explanation: Option (a) is incorrect because new ColorPencil() tries to invoke the
no-argument constructor of the class ColorPencil, which isn’t defined in the class
ColorPencil.
Option (b) is incorrect because new ColorPencil(RED) tries to pass a variable RED,
which isn’t defined in the code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What is the output of the following code? (Select 1 option.)
class Doctor {
    protected int age;
    protected void setAge(int val) { age = val; }
    protected int getAge() { return age; }
}
class Surgeon extends Doctor {
Surgeon(String val) {
specialization = val;
    }
String specialization;
String getSpecialization() { return specialization; }
}
class Hospital {
    public static void main(String args[]) {
Surgeon s1 = new Surgeon("Liver");
Surgeon s2 = new Surgeon("Heart");
s1.age = 45;
System.out.println(s1.age + s2.getSpecialization());
System.out.println(s2.age + s1.getSpecialization());
    }
}
a 45Heart
0Liver
b 45Liver
0Heart
c 45Liver
45Heart
d 45Heart
45Heart
e Class fails to compile
A
Answer: a
Explanation:  The  constructor  of  the  class Surgeon  assigns  the  values "Liver"  and
"Heart"  to  the  variable specialization  of  objects s1  and s2.  The  variable age  is
protected  in  the  class Doctor.  Also,  the  class Surgeon  extends  the  class Doctor.
Hence,  the  variable age  is  accessible  to  reference  variables s1  and s2.  The  code
assigns a value of 45 to the member variable age of reference variable s1. The variable
age of reference variable s2 is initialized to the default value of an int, which is 0.
Hence, the code prints the values mentioned in option (a).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the output of the following code? (Select 1 option.)
class RocketScience {
public static void main(String args[]) {
int a = 0;
while (a == a++) {
a++;
System.out.println(a);
}
}
}
a The while loop won’t execute; nothing will be printed.
b The while loop will execute indefinitely, printing all numbers, starting from 1.
c The while loop will execute indefinitely, printing all even numbers, starting
from 0.
d The while loop will execute indefinitely, printing all even numbers, starting
from 2.
e The while loop will execute indefinitely, printing all odd numbers, starting
from 1.
f The while loop will execute indefinitely, printing all odd numbers, starting
from 3.

A

Answer: d
Explanation: The while loop will execute indefinitely because the condition a == a++
will always evaluate to true. The postfix unary operator will increment the value of the
variable a after it’s used in the comparison expression. a++ within the loop body will
increment the value of a by 1. Hence, the value of a increments by 2 in a single loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Given the following statements,
■ com.ejava is a package
■ class Person is defined in package com.ejava
■ class Course is defined in package com.ejava
which of the following options correctly import the classes Person and Course in the
class MyEJava? (Select 3 options.)
a import com.ejava.*;
class MyEJava {}
b import com.ejava;
class MyEJava {}
c import com.ejava.Person;
import com.ejava.Course;
class MyEJava {}
d import com.ejava.Person;
import com.ejava.*;
class MyEJava {}
A
Answer: a, c, d
Explanation: Option (a) is correct. The statement import com.ejava.*; imports all
the public members of the package com.ejava in the class MyEJava.
 Option (b) is incorrect. Because com.ejava is a package, to import all the classes
defined in this package, the package name should be followed by .*:
import com.ejava.*;
Option (c) is correct. It uses two separate import statements to import each of the
classes Person and Course individually, which is correct.
Option (d) is also correct. The first import statement imports only the class Person
in MyClass. But the second import statement imports both the Person and Course
classes from the package com.ejava. You can import the same class more than once in
a Java class with no issues. This code is correct. 
 In Java, the import statement makes the imported class visible to the Java compiler,
allowing it to be referred to by the class that’s importing it. In Java, the import state-ment doesn’t embed the imported class in the target class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
Given that the following classes Animal and Forest are defined in the same
package, examine the code and select the correct statements (select 2 options).
line1>    class Animal {
line2>        public void printKing() {
line3>            System.out.println("Lion");
line4>        }
line5>    }
line6>    class Forest {
line7>        public static void main(String... args) {
line8>            Animal anAnimal = new Animal();
line9>            anAnimal.printKing();
line10>        }
line11>    }
a The class Forest prints Lion.
b If the code on line 2 is changed as follows, the class Forest will print Lion:
private void printKing() {
c If the code on line 2 is changed as follows, the class Forest will print Lion:
void printKing() {
d If the code on line 2 is changed as follows, the class Forest will print Lion:
default void printKing() {
A

Answer: a, c
Explanation: Option (a) is correct. The code will compile successfully and print Lion.
Option (b) is incorrect. The code won’t compile if the access modifier of the
method printKing is changed to private. private members of a class can’t be
accessed outside the class.
Option (c) is correct. The classes Animal and Forest are defined in the same
package, so changing the access modifier of the method printKing to default access will still make it accessible in the class Forest. The class will compile successfully
and print Lion.
Option (d) is incorrect. “default” isn’t a valid access modifier or keyword in Java. In
Java, the default accessibility is marked by the absence of any explicit access modifier.
This code will fail to compile.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
Given the following code,
class MainMethod {
    public static void main(String... args) {
System.out.println(args[0]+":"+ args[2]);
    }
}
what is 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
A

Answer: h
Explanation: This question tests you on multiple points.
1 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]: 23
args[2]: 4-3
args[3]: 5+1
2 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.
3 + 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly