0% found this document useful (0 votes)
18 views

255 Java Interview Success Questions_ Dive Deep Into Java Theory, Syntax, And APIs, And Interview With Confidence

The document '255 Java Interview Success Questions' provides a comprehensive set of 255 questions and answers designed to prepare candidates for Java-related job interviews, covering key topics such as Java basics, data types, and exception handling. The author, Jonathan Middaugh, is an experienced software engineer who created this resource while preparing for the Oracle Certified Associate exam. The book aims to enhance proficiency in Java for both aspiring and current application developers.

Uploaded by

findurmilarai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

255 Java Interview Success Questions_ Dive Deep Into Java Theory, Syntax, And APIs, And Interview With Confidence

The document '255 Java Interview Success Questions' provides a comprehensive set of 255 questions and answers designed to prepare candidates for Java-related job interviews, covering key topics such as Java basics, data types, and exception handling. The author, Jonathan Middaugh, is an experienced software engineer who created this resource while preparing for the Oracle Certified Associate exam. The book aims to enhance proficiency in Java for both aspiring and current application developers.

Uploaded by

findurmilarai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 547

255 Java Interview Success Questions

Dive Deep Into Java Theory, Syntax, and APIs, and


Interview with Confidence
Text copyright © 2020 Jonathan Middaugh all rights reserved.
Introduction
"255 Java Interview Success Questions" contains 255 rigorous questions and
answers formulated to prepare you to interview for a Java position. The
questions are randomly assorted.
This book covers nine primary topics:
•Java Basics
•Working with Java Data Types
•Using Operators and Decision Constructs
•Creating and Using Arrays
•Using Loop Constructs
•Working with Methods and Encapsulation
•Working with Inheritance
•Handling Exceptions
•Working with Selected Classes from the Java API
Proficiency in these areas is highly recommended for anyone looking for or
who has just started an application development job.
Please scroll up and sample the book, or just take the leap and purchase this
budget friendly tool!

About the Author


I earned my Masters of Science in Information Systems in 2012 and have
been a software engineer at Fortune 500 companies since then. I wrote the
original version of this book as I practiced for the Oracle Certified Associate
exam, and I hope that others can find it useful as well.
I also blog about developer careers, side hustles, and code at
https://smartdevpreneur.com/.
Table of Contents
Test 1 Questions
Test 2 Questions
Test 3 Questions
Test 4 Questions
Test 5 Questions
Test 1 Answers
Test 2 Answers
Test 3 Answers
Test 4 Answers
Test 5 Answers
Test 1 Questions
Q1.
package Exam;
public class Test extends TestAbstract {
public static void main(String[] args) {}
public void methodRun(){}
public void methodHide(){}
}
abstract class TestAbstract {
abstract void methodRun();
abstract void methodHide();
}
True or False: Test class needs an abstract modifier in order to compile.
A) True
B) False
Q1 Answered.
Q2.
package Exam;
public class ArrayCopy {
public static void main(String[] args) {
char[] copyFrom = {'c', 'r', 'u', 'n', 'c', 'h', 'y', ' ', 'c', 'o', 'o', 'k',
'i', 'e' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(copyTo);
}
}
What will the code print? Select 1 option.
A) “runchy”
B) “runchy c”
C) “unchy c”
D) “unchy co”
E) “crunchy”
Q2 Answered.
Q3.
package Exam;
public class q1 {
public static void main(String[] args) {
int[][] a = new int[5][];
//Line 1
}
}
Which of the following could be placed at line 1 and the code would still
compile? Select 1 answer.
A) a[1] = new short[5];
B) a[1] = new int[5];
C) a[1] = new Integer[5];
D) a[1] = new double[5];
E) a[1] = new int[];
Q3 Answered.
Q4.
Given the following code:
package Exam;
public class TC {
static int i2[] = new int[1];
public static void main(String[] args) {
i2[1] = 0;
System.out.println(i2[].getClass());
}
}
What will be the output? Select 1 option.
A) “class java.lang.int”
B) Does not compile
C) Runtime exception
Q4 Answered.
Q5.

//The following code is in file Go.java.


package Exam;
public class Go {
public static void main(String[] args) {
Test t1 = new TestSub();
t1.methodGo();
}
}
public class Test{
public void methodGo(){
System.out.println("In Super");
}
}
public class TestSub extends Test{
public void methodGo(){
System.out.println("In Sub");
}
}
What will this code print? Select 1 option.
A) Does not compile.
B) “In Super”
C) “In Sub”
D) Runtime Exception
Q5 Answered.
Q6.
package Exam;
public class Construct extends SuperConstruct{
Construct(int i) {
super(i);
}
Construct(String i) {
super(i);
}
Construct() {
super();
}
public static void main(String[] args) {
Construct c = new Construct();
}
}
class SuperConstruct{
SuperConstruct(int i){
System.out.println("Constructed with integer");
}
SuperConstruct(String str){
System.out.println("Constructed with string");
}
}
What will the following code print? Select 1 option.
A) “Constructed with integer”
B) “Constructed with string”
C) It will print nothing.
D) It will not compile.
E) It will throw a runtime exception.
Q6 Answered.
Q7.
When constructing a service, to what file do we add ‘provides’ syntax?
A) service.java
B) module.java
C) module-info.java
D) main.java
Q7 Answered.
Q8.
package Exam;
public class Test {
public static void main(String[] args) {
Integer i = new Integer(12);
Long l = new Long(12);
Double d = new Double(12);
System.out.println(i.equals(l));
System.out.println(l.equals(d));
}
}
What is the output from the program above? Select 1 option.
A) “true” followed by “true”
B) “false” followed by “true”
C) “false” followed by “false”
D) “true” followed by “false”
E) Does not compile.
Q8 Answered.
Q9.
package Exam;
public class Test {
public static void main(String[] args) {
System.out.println("Beginning");
Test q = new Test();
q.throwException();
System.out.println("Past finally block");
}
public void throwException(){
try{
int[] arr = new int[5];
int i = arr[5];
}finally{
System.out.println("In finally block");
}
}
}
What is the last text printed by the code? Select one option.
A) Does not compile.
B) “Beginning”
C) “In finally block”
D) “Past finally block”
E) Nothing is printed because of the runtime exception.
Q9 Answered.
Q10.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
q.go(1, "Will", " this", “ work?” );
}
public void go(int i, String...k){
for(String l:k){
System.out.print(l);
}
}
}
What does the code print? Select one option.
A) Compile time error
B) Runtime error
C) “Will this work?”
Q10 Answered.
Q11.
package Exam;
public class GC {
int g = 10;
short c = 20;
public static void main(String[] args) {
GC gc = new GC();//Line 1
gc.g = 12;
gc.c = 18; //Line 2
gc.go(); //Line 3
System.gc(); //Line 4
System.out.println(gc.g); //Line 5
}
public void go(){
System.out.println("go");
}
}
After which line is object gc eligible for garbage collection?
Select 1 option.
A) Line 1
B) Line 2
C) Line 3
D) Line 4
E) Line 5
Q11 Answered.
Q12.
package Exam;
import java.util.ArrayList;
import java.util.List;
public class Test {
List<String> dogs = new ArrayList<String>();
public static void main(String[] args) {
String dog1 = "German Shephard";
String dog2 = "Husky";
String dog3 = "Mutt";
Character dog4 = new Character('Shi Tzu');
Test q = new Test();
q.dogs.add(dog1);
q.dogs.add(dog2);
q.dogs.add(dog3);
q.dogs.add(dog4);
for(int i = 0; i<q.dogs.size(); i++){
System.out.print(q.dogs.get(i) + ",");
}
}
}
What will the output be from the above program? Select 1 answer.
A) “German Shephard,Husky, Mutt, Shi Tzu”
B) “German Shephard,Husky, Mutt, Shi Tzu,”
C) Compile time error.
D) None of the above
Q12 Answered.
Q13.
package Exam;
//Line 1
public class Test {
public static void main(String[] args) {
out.println("Hello World");
}
}
What can be inserted independently at Line 1 to make the following code
compile?
A) import java.lang.System.out;
B) import static java.lang.System.out;
C) import java.lang.System.*;
D) import static java.lang.System.*;
Q13 Answered.
Q14.
package Exam;
public class Test{
Object obj = new Object();
Integer i = new Integer(45);
public static void main(String[] args) {
Test q = new Test();
q.obj = "Hello";
q.obj = q.i;
System.out.println(q.obj);
}
}
What will the program print? Select 1 option.
A) “45”
B) Does not compile.
C) “Hello”
D) Runtime Error
Q14 Answered.
Q15.
package Exam;
public class FooledYou implements Fool {
static int r = 12;
public static void main(String[] args) {
Fool fy = new FooledYou;
fy.methodGo(Fool.r);
System.out.println(r);
}
public void methodGo(int r){
System.out.println(r);
}
}
interface Fool{
void methodGo(int r);
int r = 10;
}
What does the code print? Select 1 answer.
A) “12” followed by “12”
B) “10” followed by “10”
C) “10” followed by “12”
D) “12” followed by “10”
E) Does not compile.
Q15 Answered.
Q16.
What will force a consuming module to import dependencies of a consumed
module?
A) requires transitive com.module
B) requires dependeny com.module
C) import transitive com.module
D) import dependency com.module
Q16 Answered.
Q17.
Which of the following is not a valid class modifier? Select 1 option.
A) public
B) private
C) protected
D) package private
E) package protected
Q17 Answered.
Q18.
package Exam;
public class Test {
String st;
int x = 10;
public static void main(String[] args) {
Test q = new Test();
System.out.println("Howdy");
if(q.x >5 | q.st.length() == 0){
System.out.println("Hello");
}
}
}
What does the code print? Select one option.
A) Does not compile.
B) “Howdy” followed by “Hello”
C) “Howdy”
D) Runtime Exception causes program to fail immediately.
E) Prints "Howdy" and then a Runtime Exception causes program failure.
Q18 Answered.
Q19.
package Exam;
public class Test {
public static void main(String[] args) {
int i = 12;
long L = 12;
float f = 12.0f;
double d = 12.2;
System.out.print(i == f + “ “);
System.out.print(i == d + “ “);
System.out.print(i.equals(f) + “ “);
}
}
What will the following program print? Select 1 answer.
A) Does not compile.
B) “false false false”
C) “true true true”
D) “true false true”
E) “true false false”
Q19 Answered.
Q20.
package Exam;
public class Go {
public static void main(String[] args) {
TestSub ts = new TestSub();
ts.runThis();
}
}
class Test{
static{System.out.println("Super static");} //Line 1
{System.out.println("Super non-static");} //Line 2
public void runThis(){
System.out.println("Super runThis"); //Line 3
}
}
class TestSub extends Test{
static{System.out.println("Sub static");} //Line 4
public void runThis(){
System.out.println("Sub runThis"); //Line 5
}
{System.out.println("Sub non-static");} //Line 6
}
What is the following order of output? Select 1 answer.
A) 1, 2, 4, 5, 6
B) 4, 5, 6
C) 4, 6, 5
D) 1, 6, 4, 2, 5
E) 1, 4, 2, 6, 5
Q20 Answered.
Q21.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
q.subMethod();
}
public void subMethod(){
try{
subMethod2();
}catch(Exception e){
System.out.println("The Exception was caught here");
}
}
public void subMethod2(){
throw new ArrayIndexOutOfBoundsException();
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException”
C) “The Exception was caught here”
Q21 Answered.
Q22.
package Exam;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List l = new ArrayList();
l.ensureCapacity(20);
System.out.print(l.size());
for(int i = 0; i<10; i++){
l.add(i);
}
l.trimToSize();
System.out.print(“ “ + l.size());
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “20 10”
C) “20 20”
D) “0 10”
Q22 Answered.
Q23.
package Exam;
public class Test {
public static void main(String[] args) {
double d1 = 12.4;
double d2 = 12.40;
System.out.print(" " + Double.compare(d1, d2));
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “0”
C) “0”
Q23 Answered.
Q24.
Which of the following are acceptable declarations? Select two answers.
A) String String;
B) String 123;
C) String volatile;
D) String $$$MONEY
E) String $$$money!!!
Q24 Answered.
Q25.
package Exam;
public class Test extends qqq {
public Test(int i, int j, Integer integer) {
//Line 1
}
public static void main(String[] args) {
Test q = new Test(1,2,new Integer("5"));
}
}
class qqq{
public int aa;
public int bb;
public int cc;
public qqq(int a, int b, Integer c){
int aa = a;
int bb = b;
int cc = c.intValue();
}
}
What is needed at Line 1? Select 1 answer.
A) A call to the default constructor of qqq.
B) A call to the explicit constructor qqq(int a, int b, Integer c)
C) Nothing is needed.
Q25 Answered.
Q26.
package Exam;
public class Test extends SuperTest{
public static void main(String[] args) {
Test q = new Test();
try {
q.run();
} catch (Exception e) {
e.printStackTrace();
}
}
void run() throws Exception{
System.out.println("Overriding methods can throw more specific
exceptions");
}
}
class SuperTest{
void run() throws FileNotFoundException{
System.out.println("Overridden methods must throw more general
exceptions");
}
}
What is the output of the code? Select 1 answer.
A) “Overriding methods can throw more specific exceptions”
B) “Overridden methods must throw more general exceptions”
C) Does not compile
Q26 Answered.
Q27.
package Exam;
public class Test extends SuperTest {
public static void main(String[] args) {
Test q = new Test();
q.returnThis();
}
public Number returnThis(){
Number doub = 2.2;
System.out.println(doub);
return integ;
}
}
class SuperTest{
public Double returnThis(){
Double num = 3d;
return num;
}
}
What does the following code print? Select one of the following.
A) “2.2”
B) “3”
C) Does not compile
Q27 Answered.
Q28.
package Exam;
import java.util.ArrayList;
public class Test {
ArrayList trainList = new ArrayList();
public static void main(String[] args) {
Test q = new Test (); //Line 1
q.addTrain("A1","Fort Worth","Amarillo",
q.trainList); //Line 2
q.addTrain("A2","Belen","Los Angeles",q.trainList);
q.addTrain("A3","Chicago","Kansas City",q.trainList);
System.out.println(((Train) q.trainList.get(1)).getTrainID());
}
public void addTrain(String id, String dep, String dest, ArrayList
tL){
Train trn = new Train(); //Line 3
trn.setTrainID(id);
trn.setDeparture(dep);
trn.setDestination(dest);
tL.add(trn); //Line 4
}
}
class Train{
private String trainID;
private String departure;
private String destination;
public String getTrainID() {
return trainID;
}
public void setTrainID(String trainID) {
this.trainID = trainID;
}
public String getDeparture() {
return departure;
}
public void setDeparture(String departure) {
this.departure = departure;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
}
Which line causes an Exception? Select one of the following.
A) Line 1
B) Line 2
C) Line 3
D) Line 4
E) The code is fine
Q28 Answered.
Q29.
package Exam;
import java.util.ArrayList;
import java.util.List;
public class Test{
List<Train> trainList = new ArrayList<Train>();
public static void main(String[] args) {
Test q = new Test();
q.addTrain("A1","Fort Worth","Amarillo", q.trainList);
q.addTrain("A2","Belen","Los Angeles",q.trainList);
q.addTrain("A3","Chicago","Kansas City",q.trainList);
System.out.println(q.trainList.get(1).getTrainID());
}
public void addTrain(String id, String dep, String dest, List<Train>
tL){
Train trn = new Train();
trn.setTrainID(id);
trn.setDeparture(dep);
trn.setDestination(dest);
tL.add(trn);
}
}
class Train{
private String trainID;
private String departure;
private String destination;
public String getTrainID() {
return trainID;
}
public void setTrainID(String trainID) {
this.trainID = trainID;
}
public String getDeparture() {
return departure;
}
public void setDeparture(String departure) {
this.departure = departure;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
}
What does the following code print? Select one of the following.
A) “A1”
B) “A2”
C) “A3”
D) Does not compile
Q29 Answered.
Q30.
package Exam;
public class Test {
public static void main(String[] args) {
Integer in = new Integer(5);
Double db = new Double(5.5);
System.out.println(in.compareTo(db));
}
}
What does the following code print? Select one of the following.
A) “1”
B) “0”
C) “-1”
D) Does not compile
Q30 Answered.
Q31.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
Test q = new Test();
ArrayList tr = new ArrayList();
for(int i = 0; i<3; i++){
tr.add(new StringBuilder("str" + i));
}
q.change(tr);
System.out.println(tr.get(1));
}
public void change(ArrayList ls){
for(int i = 0; i < ls.size(); i++){
((StringBuilder) (ls.get(i)).append(" is here");
}
}
}
What does the following code print? Select one of the following.
A) “str0”
B) “str0 is here”
C) “str1”
D) “str1 is here”
E) Does not compile
Q31 Answered.
Q32.
True or false? In java, object parameters are passed by value?
A) True
B) False
Q32 Answered.
Q33.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
do{
int i = 0;
i++;
}while(i < 10);
System.out.println(i);
}
}
What does the following code print? Select one of the following.
A) “10”
B) “9”
C) Does not compile
Q33 Answered.
Q34.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
int i = 0;
while(i < 10)
do{
i++;
}
System.out.println(i);
}
}
What does the following code print? Select one of the following.
A) “10”
B) “9”
C) Does not compile
Q34 Answered.
Q35.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
int counter = 0;
for(int i = 5; i > 0; i--){
for(int x = 0; x < i; x++){
counter++;
}
}
System.out.println(counter);
}
}
What does the following code print? Select one of the following.
A) “5”
B) “10”
C) “15”
D) “20”
E) “25”
Q35 Answered.
Q36.
package Exam;
import java.util.ArrayList;
public class Test{
public static void main(String[] args) {
ArrayList arr = new ArrayList();
int counter = 0;
outer: for(int i = 5; i > 0; i--){
inner: for(int x = 0; x < i; x++){
if(x == 3){
break inner;
}
counter++;
}
}
System.out.println(counter);
}
}
What does the following code print? Select one of the following.
A) “10”
B) “12”
C) “14”
D) Does not compile
Q36 Answered.
Q37.
package Exam;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");;;;;;;;;;
}
}
True or false? This code compiles.
A) True
B) False
Q37 Answered.
Q38.

True or False? By default, java modules expose all of their API to other
modules?
A) True
B) False
Q38 Answered.
Q39.
package Exam;
public class Test{
public static void main(String[] args) {
Test q = new Test();
StringBuilder sb = new StringBuilder("This");
q.changeThis(sb);
System.out.println(sb);
}
public void changeThis(StringBuilder str){
System.out.print(str + " ");
str.delete(0, 4);
str.append("That");
}
}
What does the following code print? Select one of the following.
A) “This That”
B) “That That”
C) “This This”
D) Does not compile
Q39 Answered.
Q40.
Which of the following are true of getters and setters? Select 2 options.
A) Getters and setters increase code reusability
B) Getters and setters are an integral part of Object Oriented Programing
C) Getters and setters are control the flow of the program
D) Getters and setters maintain encapsulation
Q40 Answered.
Q41.
What is an interface with a single abstract method called?
A) It doesn’t have a name
B) Method Interface
C) Functional Interface
D) Abstract Interface
Q41 Answered.
Q42.
package Exam;
@FunctionalInterface
interface Cube {
int calculateVolume(int x);
int countSides(int y);
}
True or False: This code compiles
A) True
B) False
Q42 Answered.
Q43.
package Exam;
interface TestInterface {
public void add(int x);
default void print() {
System.out.println("Default Method Executed");
}
default void printAnother() {
System.out.println("Another Default Method Executed");
}
}
public class Test implements TestInterface {
public void add(int x) {
System.out.println(x+x);
}
public static void main(String args[]) {
Test writer = new Test();
writer.add(4);
writer.print();
}
}
True or False: This code compiles
A) True
B) False
Q43 Answered.
Q44.
True or False: Static methods on an interface can be overridden
A) True
B) False
Q44 Answered.
Q45.
package Exam;
interface LambdaInterface {
public void printer(String x);
default void printAnother() {
System.out.println("Another Print!“);
}
}
public class Test {
public static void main(String args[]) {
LambdaInterface inter = (String x) ->
System.out.println(x);
inter.printer(“Print Me!”);
}
}
What does the following code print? Select one of the following.
A) “Print Me!”
B) “Another Print!”
C) Does not compile
Q45 Answered.
Q46.
package Exam;
interface LambdaInterface {
public void printer(String x);
public void anotherPrinter();
default void printAnother() {
System.out.println("Another Print!“);
}
}
public class Test {
public static void main(String args[]) {
LambdaInterface inter = (String x) ->
System.out.println(x);
LambdaInterface anotherInter = () ->
System.out.println("Print This!”);
inter.printer(“Print Me!”);
}
}

What does the following code print? Select one of the following.
A) “Print Me!”
B) “Print This!”
C) “Another Print!”
D) Does not compile
Q46 Answered.
Q47.
package Exam;
import java.clock.LocalDate;
public class HelloWorld {
public static void main(String []args){
LocalDate local = LocalDate.new();
System.out.println(local);
}
}
What does the following code print? Select one of the following.
A) The current time
B) The current date
C) The current time and date
D) Does not compile
Q47 Answered.
Q48.
Which of the following is not a valid java.time.LocalDate method?
A) atTIme
B) equals
C) getDayOfYear
D) getWeekDay
E) isLeapYear
Q48 Answered.
Q49.
What is the default value for the Boolean object?
A) true
B) false
C) undefined
D) null
Q49 Answered.
Q50.
What is the size in bytes of the int primitive?
A) 1 byte
B) 2 bytes
C) 4 bytes
D) 8 bytes
Q50 Answered.
Q51.
Which syntax creates an optional, compile-time-only dependency?
A) requires devDependency com.module
B) requires static com.module
C) import optional com.module
Q51 Answered.
Q52.
package Exam;
public class Test{
public static void main(String[] args) {
Test q = new Test();
StringBuilder sb = new StringBuilder("This");
q.changeThis(sb);
System.out.println(sb);
}
public void changeThis(StringBuilder str){
System.out.print(str + " "); //Line 1
str = new StringBuilder("That"); //Line 2
}
}
What does the following code print? Select one of the following.
A) “This That”
B) “That That”
C) “This This”
D) Does not compile
Q52 Answered.
Q53.
The following code snippet is valid.
static int i1, i2[], i3;
Select 1 option.
A) True
B) False
Q53 Answered.
Q54.
package Exam;
public class Test {
public static void main(String[] args) {
float f = 5.0;
Test q = new Test ();
q.runInt(f);
}
public void runInt(float f){
Integer i = new Integer(new Float(f).intValue());
System.out.print(i );
}
}
What is the output of the code? Select one option.
A) “5”
B) “5.0”
C) Compile time exception
D) Runtime exception
Q54 Answered.
Q55.
What command line command will show all modules?
A) java --show-modules
B) java --list-modules
C) java --module-chart
D) java --modules
Q55 Answered.
Test 2 Questions
Q1.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
String s = "hello";
StringBuilder sb = new StringBuilder("world");
System.out.println(q.addString(s));
System.out.println(q.addSB(sb));
System.out.println(s);
System.out.println(sb);
}
public String addString(String str){
str += " world";
return str;
}
public StringBuilder addSB(StringBuilder strbl){
strbl.insert(0, "hello ");
return strbl;
}
}
What will the code print?
A) "hello world" four times
B) "hello" followed by "hello world" followed by "hello" followed by "hello
world"
C) "hello" followed by "world" followed by "hello" followed by "world"
D) "hello world" twice followed by "hello" followed by "hello world"
Q1 Answered.
Q2.
package Exam;
public class Test {
public static void main(String[] args) {
switch(2){
case 1: System.out.println("1");
case 2: System.out.println("2");
case 3: System.out.println("3");
default: System.out.println("None of the above");
}
}
}
What does the code print? Select one option.
A) Prints "2"
B) Prints "1", "2", "3", "None of the above"
C) Prints "2", "3"
D) Prints "2", "3", "None of the above"
E) Does not compile.
Q2 Answered.
Q3.
package Exam;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new
FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append('\n');
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();
}
}catch (Exception e) {
System.out.println("Exception caught");
}catch (IOException e) {
System.out.println("IOException caught");
}
}
}
If file.txt is null, what will the code generate? Select 1 option.
A) The code will print "Exception caught"
B) The code will not compile even if file.txt is not null.
C) The code will print "IOException caught"
D) The code will throw an exception that is not caught, causing the program
to end.
Q3 Answered.
Q4.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
q.methodRun();
System.out.println("Complete");
}
public void methodRun();
public void methodHide(){
}
}
abstract class TestAbstract {
abstract void methodRun();
abstract void methodHide();
abstract void methodSeek();
}
What is the output from this program? Select one option.
A) “Complete”
B) Does not compile
C) Run time error
Q4 Answered.
Q5.
package Exam
public class Test {
public static void main(String[] args) {
int[][] myInt = {{2, 3, 1}, {4, 5, 6}, {7, 8, 9}};
System.out.println(myInt[1][1]);
System.out.println(myInt[3][1]);
}
}
What is the output from the code? Choose one answer.
A) “5” followed by ArrayIndexOutOfBoundsException
B) “2” followed by “7”
C) “2” followed by “1”
D) “5” followed by “7”S
E) Does not compile
Q5 Answered.
Q6.
Which of these are valid ArrayList object methods? Select 3 options.
A) .size()
B) .isEmpty()
C) .length()
D) .contains()
E) .removeElement()
Q6 Answered.
Q7.
package Exam;
import java.sql.Array;
import java.util.ArrayList;
public class Test {
Short a = new Short((short) 1);
Integer b = new Integer(1);
public static void main(String[] args) {
Test t = new Test();
if (t.b instanceof Short){
System.out.println("Test");
}else System.out.println("Failed");
}
}
What will the code print? Select 1 option.
A) “Test”
B) “Failed”
C) Will not compile.
D) Runtime Exception
E) Prints nothing.
Q7 Answered.
Q8.
package Exam;
public class Test {
int i = 0;
public static void main(String[] args) {
System.out.println("Hello World");
}
}
True or False: i is a class variable
A) True
B) False
Q8 Answered.
Q9.
package Exam;
import java.util.ArrayList;
import java.util.List;
public class Test {
List<String> dogs = new ArrayList<String>();
public static void main(String[] args) {
String dog1 = "German Shephard";
String dog2 = "Husky";
String dog3 = "Mutt";
Character dog4 = new Character('s');
Test q = new Test();
q.dogs.add(dog1);
q.dogs.add(dog2);
q.dogs.add(dog3);
for(int i = 0; i<dogs.size(); i++){
System.out.print(q.dogs.get(i) + ",");
}
}
}

What will the output be from the above program? Select 1 answer.
A) “German Shephard,Husky, Mutt,”
B) “German Shephard,Husky, Mutt”
C) Compile time error.
D) None of the above
Q9 Answered.
Q10.
package Exam;
public class Test {
public static void main(String[] args) {
Integer i = new Integer(1); //Line 1
Number n = i; //Line 2
System.out.println(n.intValue()); //Line 3
i = n; //Line 4
System.out.println(i.intValue()); //Line 5
}
}
What can be changed to make this code compile? Select one option.
A) Change line 4 to i = (Integer) n;
B) Change line 2 to n = (Number) i;
C) Change the method called in Line 3 and 5 to doubleValue()
D) Change line 5 to System.out.println(i.parseInt("1"));
E) No changes need to be made.
Q10 Answered.
Q11.
package Exam;
public class Test {
int i = 0;
public static void main(String[] args) {
//Line 1
for(i = 0; i<10; i++){
System.out.println("Hello World");
}
}
}
What changes can be made to the following code so that it will compile?
Select 2 options.
A) No changes are required to compile
B) Insert the following code at Line 1: Test q = new Test(); Then change i to
q.i
C) Declare i as type int within the for statement
D) No changes are required to run
E) Replace 'i = 0;' with 'i;'
Q11 Answered.
Q12.
package Exam;
public class B extends A {
public static void main(String[] args) {
A q = new B();
q.methodA1();
q.methodA2();
}
static void methodA1(){
System.out.print("Static B1,");
}
void methodA2(){
System.out.print(" B2");
}
}
class A{
static void methodA1(){
System.out.print("Static A1,");
}
void methodA2(){
System.out.print(" A2");
}
}
What does the code print? Select one option.
A) “Static A1, A2”
B) “Static A1, B2”
C) “Static B1, A2”
D) “Static B1, B2”
Q12 Answered.
Q13.
package Exam;
public class TestSub extends Test {
public static void main(String[] args) {
Test q = new TestSub();
q.out();
}
public void out(){
System.out.println("Sub out");
}
}
class Test {
protected void out(){
System.out.println("Super out");
}
}
What will the code print? Select 1 option.
A) Does not compile.
B) “Sub out”
C) “Super out”
D) Prints nothing.
Q13 Answered.
Q14.
True or False: The ! operator returns a boolean. Select one option.
A) True
B) False
Q14 Answered.
Q15.
package Exam;
public class Test {
public static void main(String[] args) {
double r = Math.random() * 10; //Line 1
Integer x = new Integer((int)r); //Line 2
System.out.println(x); //Line 3
}
}
What can be added to the following code to make it compile? Select 1
option.
A) It already compiles.
B) Append .intValue() to Integer(r).
C) Declare r as int in line 1.
D) Cast r as int in line 3.
Q15 Answered.
Q16.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
String s = "hello world";
System.out.println(s.equals("hello world"));
System.out.println(s.concat(s.charAt(0)));
}
}
What will the code print? Select 1 option.
A) "true" followed by "hello worldh"
B) "false" followed by "hello worldh"
C) Compile time error
D) "true" and the a runtime error is thrown
Q16 Answered.
Q17.
package Exam;
public class Test {
public static void main(String[] args) {
int[][] myInt[] = {{{1,2},{3,4}},{{1,2},{3,4,5},{6}},{{7}}};
System.out.println(myInt[0][1][0]);
System.out.println(myInt[1][1][2]);
System.out.println(myInt[2][0][0]);
}
}
What is the output from the code? Choose one answer.
A) “5” followed by ArrayIndexOutOfBoundsException
B) “2” followed by “7” followed by “1”
C) “2” followed by “1” followed by “6”
D) Does not compile
E) “3” followed by “5” followed by “7”
Q17 Answered.
Q18.
Which of the following correctly describe a method's signature? Select 1
option.
A) The method's access modifier, return type, name, and parameters.
B) The method's return type, name, and parameters.
C) The method's name and parameters.
D) The method's name.
Q18 Answered.
Q19.
package Exam;
public class TestSub extends Test {
public static void main(String[] args) {
Test q = new TestSub();
q.out();
}
public void out(){
System.out.println("Sub out");
}
}
class Test {
private void out(){
System.out.println("Super out");
}
}
What will the code print? Select 1 option.
A) Does not compile.
B) “Sub out”
C) “Super out”
D) Prints nothing.
Q19 Answered.
Q20.
package Exam
public class Test {
String str1 = "Hello World!";
String str2;
public static void main(String[] args) {
Test q = new Test();
if (q.str1.equals("Hello World!") | q.str2.equals("")){
System.out.println(q.str1);
}else System.out.println(q.str2.concat("Hello"));
}
}
What does the code print? Select one answer.
A) "Hello World!"
B) Compile time error
C) The code prints a blank line
D) "Hello"
E) Runtime Exception
Q20 Answered.
Q21.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
try{
q.subMethod();
}catch(Exception e){
System.out.print(" …Or the Exception was caught here");
}
}
public void subMethod(){
subMethod2();
System.out.print("The Exception was handled in subMethod2");
}
public void subMethod2(){
throw new ArrayIndexOutOfBoundsException();
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “The Exception was handled in subMethod2 …Or the Exception was
caught here”
C) "The Exception was handled in subMethod2"
D) " …Or the Exception was caught here"
Q21 Answered.
Q22.
package Exam;
public class Test {
public static void main(String[] args) {
j = 0;
int j;
StringBuilder newString = new StringBuilder("");
while(j<4.5){
newString.append("Hello World".substring(j, j+1));
j++;
}
System.out.println(newString);
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “Hello”
C) "Heelllloo "
D) ""
Q22 Answered.
Q23.
package Exam;
public class Test {
public static void main(String[] args) {
int j;
j = 0;
String newString = new String("");
while(j<=5){
newString.concat("Hello World".substring(j, j+1));
j++;
}
System.out.println(newString);
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “Hello ”
C) "Hello"
D) ""
Q23 Answered.
Q24.
package Exam;
public class Test extends Abs { //Line 1
public static void main(String[] args) {
}
public void doThis(){
}
public void doThat(){
}

}
abstract class Abs implements Ione, Itwo{ //Line 2
}
interface Ione{
public void doThis(); //Line 3
}
interface Itwo{
public void doThat(){ //Line 4
}; //Line 5
}
Where is the error in the code? Select 1 answer.
A) Line 1
B) Line 2
C) Line 3
D) Line 4
E) Line 5
Q24 Answered.
Q25.
package Exam;
public class Test {
public static void main(String[] args) {
double d1;
if(d1<3){
d1=3;
}
for(int i = 0; i < d1; i+=2, d1+=1){
System.out.print(d1);
}
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “345”
C) "3.04.05.0"
D) "3456"
E) “3.04.05.06.0”
Q25 Answered.
Q26.
package Exam;
public class Test extends SuperTest{
public static void main(String[] args) {
Test q = new Test();
try {
q.run();
} catch (Exception e) {
e.printStackTrace();
}
}
void run() throws Exception{
System.out.println("This will run");
}
}
class SuperTest{
void run() throws ArrayIndexOutOfBoundsException {
System.out.println("Actually, this will run");
}
}
What is the output of the code? Select 1 answer.
A) “This will run”
B) “Actually, this will run”
C) Does not compile
Q26 Answered.
Q27.
package Exam;
public class Test{
public static void main(String[] args) {
String str1 = new String("String 1");
String str2 = new String("String 2");
System.out.println(str2.compareTo(str1));
}
}
What is the output of the code? Select 1 answer.
A) “-1”
B) “0”
C) “1”
D) Does not compile
Q27 Answered.
Q28.
package Exam;
import java.util.ArrayList;
public class Test {
ArrayList rec = new ArrayList();
public static void main(String[] args) {
Test q = new Test();
Rectangle r1 = q.new Rectangle(2,3);
Rectangle r2 = q.new Rectangle(3,4);
Rectangle r3 = q.new Rectangle(4,5);
q.rec.add(r1);
q.rec.add(r2);
q.rec.add(r3);
for(int i = 0; i < q.rec.size(); i++){
if((i%2)==0){
System.out.println(((Rectangle) (q.rec.get(i))).getArea());
}
}
}
class Rectangle{
private int side1;
private int side2;
public void setSide1(int x){
side1 = x;
}
public int getSide1(){
return side1;
}
public void setSide2(int y){
side2 = y;
}
public int getSide2(){
return side2;
}
public int getArea(){
return side1*side2;
}
public Rectangle(int s1,int s2){
setSide1(s1);
setSide2(s2);
}
}
}
What is the output of the code? Select 1 answer.
A) “6” followed by “20”
B) “6” followed by “12” followed by “20”
C) “12”
D) Does not compile
E) Runtime exception
Q28 Answered.
Q29.
package Exam;
public class Test{
public static void main(String[] args) {
int rate = 10;
double amount = rate/100;
System.out.println(amount);
}
}
What is the output of the code? Select 1 answer.
A) “1”
B) “0”
C) “.1”
D) Does not compile
Q29 Answered.
Q30.
package Exam;
public class Test{
public static void main(String[] args) {
char c = 49;
System.out.println(c);
}
}
True or false: The code prints “49”.
A) True
B) False
Q30 Answered.
Q31.
What are the benefits of object-oriented programing (OOP)? Select three
answers.
A) Code is socketable or modular, so a class’s source code can be
maintained independently of the rest of an application’s code.
B) An object’s code is kept private, meaning other objects only interact with
an object’s methods, so the internal code is kept hidden.
C) Objects are easily reusable. They can be plugged into multiple parts of
the code.
D) Object-oriented code executes faster and computer resource usage is
minimized.
Q31 Answered.
Q32.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
try{
ArrayList arr = new ArrayList();
arr.add(0, "element 0");
System.out.println(arr.get(1));
}catch(IndexOutOfBoundsException e){
System.out.println("Cannot access. There are only " + arr.size() +
" elements.");
}
}
}
What does the code print? Select one option.
A) “element 0”
B) “Cannot access. There are only 1 elements.”
C) Does not compile
Q32 Answered.
Q33.
package Exam;
import java.util.ArrayList;
//Line 1
public class Test{
public static void main(String[] args) {
Test2 t = new Test2();
System.out.println(t.x);
}
}
class Test2{
public int x = 100;
}
True or false? Line 1 should be the code import Exam.Test2;
A) True
B) False
Q33 Answered.
Q34.
True or false? An instance level variable can be accessed anywhere in a class
as long as an instance has been created.
A) True
B) False
Q34 Answered.
Q35.
package Exam;
public class Test{
public static void main(String[] args) {
int i;
if(5%3>2){
i=0;
}else if(5%3>1){
i=1;
}
}
}
Why will this code not compile? Select one option.
A) It compiles fine
B) 5%3>2 and 5%3>1 do not return Booleans and thus cannot be used in an
if statement
C) String[] args is never used
D) i is a local variable and its value is never explicitly defined
Q35 Answered.
Q36.
package Exam;
public class Test{
public static void main(String[] args) {
int i;
int x = 2;
if(5%x>2){
i=0;
}else if(5%x>1){
i=1;
}
}
}
True or false? This code compiles.
A) True
B) False
Q36 Answered.
Q37.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
q.throwException();
}
public void throwException(){
try{
System.out.println("Get this".substring(1,9));
}catch(Exception e){
}
}
}
What does the code print? Select one option.
A) “Get this”
B) Prints nothing
C) Does not compile
Q37 Answered.
Q38.
package Exam;
public class Test{
public static void main(String[] args) {
System.out.println("Beginning");
Test q = new Test();
q.throwException();
System.out.println("Past finally block");
}
public void throwException(){
try{
catchException();
}catch(Exception e){
System.out.println("thrown");
}
}
public void catchException() throws Exception{
throw new Exception();
}
}
What is the last line the code prints? Select one option.
A) “Beginning”
B) “thrown”
C) “Past finally block”
Q38 Answered.
Q39.
Which of the following instantiates the object Dog? Select 2 options.
A) public Dog{}
B) public void Dog{}
C) public void Dog(){}
D) public Dog{};
Q39 Answered.
Q40.
True or false? A class level variable can be accessed anywhere in a class.
A) True
B) False
Q40 Answered.
Q41.
package Exam;
interface TestInterface {
public void add();
default void print() {
System.out.println("Default Method Executed");
}
}
public class Test implements TestInterface {
public void add(int x) {
System.out.println(x+x);
}
public static void main(String args[]) {
Test writer = new Test();
writer.print();
writer.add(5);
}
}
Which of the following is the output of the code? Select one option.
A) “Default Method Executed” followed by “10”
B) “10”
C) “Default Method Executed”
D) Compile time error
Q41 Answered.
Q42.
package Exam;
interface TestInterface {
default void print() {
System.out.println("Not Important");
}
static void printImportant() {
System.out.println(“Important”);
}
}
public class Test implements TestInterface {
static void printImportant() {
System.out.println(“Really Important”);
}
public static void main(String args[]) {
printImportant();
}
}
Which of the following is the output of the code? Select one option.
A) “Important” followed by “Really Important”
B) “Important”
C) “Really Important”
D) Compile time error
Q42 Answered.
Q43.
What is the default value of any Object?
A) null
B) undefined
C) 0
D) There is no default value common to all objects
Q43 Answered.
Q44.
True or False: static variables cannot be modified in non-static methods?
A) True
B) False
Q44 Answered.
Q45.
package Exam;
public class Tester{
private int myInt = 5;
public static void main(String []args){
Tester test = new Tester();
int myInt = 10;
System.out.println(test.incrementInt(test.myInt));
}
public int incrementInt (int theirInt) {
return theirInt++;
}
}
What does the code print?
A) 5
B) 6
C) 10
D) 11
Q45 Answered.
Q46.
Which of the following are true about arrays and ArrayLists? Select all
applicable answers.
A) Arrays have a fixed length and ArrayLists do not
B) ArrayList can hold primitives, arrays can only hold objects
C) ArrayList implements the Collection interface
D) Array implements the List interface
Q46 Answered.
Q47.
package Exam;
import java.util.function.Predicate;
public class PredicateTest{
public static void main(String []args){
Predicate<String> containsS = testString -> (testString.contains("s"));
System.out.println(containsS("cows"));
}
}
What does the code print?
A) true
B) false
C) “s”
D) Does not compile
Q47 Answered.
Q48.
package Exam;
import java.util.function.Predicate;
public class PredicateTest{
public static void main(String []args){
Predicate<String> containsS = testString -> (testString.contains("s"));
Predicate<String> containsT = testString -> (testString.contains("t"));
System.out.println(containsS.and(containsT).test("tricksy"));
}
}
What does the code print?
A) “s”
B) “t”
C) true
D) false
E) Does not compile
Q48 Answered.
Q49.
package Exam;
public class Tester{
public static void main(String []args){
Tester test = new Tester();
int myInt = 3;
System.out.println(++myInt++);
}
}
What does the code print?
A) 3
B) 4
C) 5
D) Does not comile
Q49 Answered.
Q50.
package Exam;
public class Tester{
private static int myInt = 1;
public static void main(String []args){
Tester test = new Tester();
int myInt = 3;
System.out.println(test.myInt);
}
}
What will the code print?
A) 1
B) 3
C) Does not compile
Q50 Answered.
Test 3 Questions
Q1.
package Exam;
public class Test {
public static void main(String[] args) {
int i = 0;
char a = 'a';
short s = 1;
i = a;
System.out.println(i);
}
}
What is printed out by the code? Select 1 option.
A) “0”
B) “1”
C) A value greater than 1
D) A value less than zero
Q1 Answered.
Q2.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
String s = " hello world ";
System.out.println(s.trim());
}
}
What will the code print? Select 1 option.
A) " hello world"
B) "hello world "
C) "hello world"
D) "helloworld"
Q2 Answered.
Q3.
Which of the following is not a valid declaration and instantiation of an
array? Select
two options.
A) int[][][] myInt = new int[1][2][3];
B) int[][][] myInt = new int[1][][];
C) int[][][] myInt = new int[][][3];
D) int[][] myInt[] = new int[1][][];
E) int[1][2][3] myInt = new int[][][];
Q3 Answered.
Q4.
package Exam;
public class Test {
public static void main(String[] args) {
String this = "Team USA";
String that = "Team UK";
this.concat(that);
System.out.println(this);
modify(this,that);
System.out.println(this);
s=modify(this, that);
System.out.println(this);
}
public static String modify(String one, String two){
String mod = "";
mod = two.concat(" and") + one.substring(4);
return mod;
}
}
Which of the following is the output of the code? Select one option.
A) “Team USA” followed by “Team USA” followed by “Team UK and
Team USA”
B) “Team USA and Team UK” followed by “Team USA and Team UK”
followed by “Team UK and Team USA”
C) “Team USA” followed by “Team USA and Team UK ” followed by
“Team UK and Team USA”
D) “Team USA and Team UK” followed by “Team USA ” followed by
“Team UK and Team USA”
E) Compile time error
Q4 Answered.
Q5.
double x = 10;
Which of the following code snippets will assign x's value to str? Select 1
option.
A) String str = x.toString();
B) Stinrg str = "";
str = x.toString();
C) String str = Double.toString(x);
D) Stinrg str = "";
str = Double.toString(x);
E) None of the above.
Q5 Answered.
Q6.
package Exam;
public class Test {
public static void main(String[] args){
int count = 1;
System.out.println(count);
System.out.println(++count);
System.out.println(count++);
System.out.println(count);
}
}
What does the code print? Select one option.
A) “1123”
B) “1122”
C) “1133”
D) “1233”
E) “1223”
Q6 Answered.
Q7.
package Exam
public class Test {
public static void main(String[] args) {
int[][][] myInt = new int[1][2][3];
int[][] yourInt = {{1,2},{3,4}};
myInt[0] = yourInt[][];
System.out.println(myInt[0][1][0]);
}
}
True or false: the code is valid.
A) True
B) False
Q7 Answered.
Q8.
package Exam;
public class Test {
static int i = 0;
static short s = 0;
static byte b = 0;
static double d = 0;
static char c = '0';
public static void main(String[] args) {
System.out.println(i == s);
System.out.println(b == d);
System.out.println(c == i);
}
}
How many times will the code print 'true'? Select 1 option.
A) 0
B) 1
C) 2
D) 3
Q8 Answered.
Q9.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
String s = "hello world";
System.out.println(s.append("!!!"));
System.out.println(s.reverse());
}
}
What will the code print? Select 1 option.
A) "hello world!!!" followed by "!!!dlrow olleh"
B) "hello world!!!" followed by "dlrow olleh"
C) "hello world" followed by "dlrow olleh"
D) Compile time error
Q9 Answered.
Q10.
package Exam;
public class Test {
String s = "hello";
StringBuilder sb = "world";
public static void main(String[] args) {
System.out.println(s.concat(sb.toString()));
}
}
What does the code print? Select one option.
A) “helloworld”
B) “worldhello”
C) Does not compile
Q10 Answered.
Q11.
package Exam;
public class Test {
public static void main(String[] args) {
int r = 0;
for(int xx = 0; xx < 10; xx+=r++){
System.out.println(++r);
}
}
}
What does the code print? Select one answer.
A) “1,3,5,7,9,”
B) “2,4,6,8,”
C) “1,2,3,4,5,6,7,8,9,”
D) “1,3,5,7,”
E) “2,4,6,8,10,”
Q11 Answered.
Q12.
package Exam;
public class Test {
public static void main(String[] args) {
for(x = 0; x < 5; ++x){
System.out.print(++x + ",");
int y = 0;
for(y;y<x;y+=x){
System.out.print(y +",");
}
}
}
}
What will the code print? Select one option.
A) Compile time error
B) “1,1,3,3,5,5,”
C) “1,0,3,0,5,0,”
D) “1,2,3,4,5,”
Q12 Answered.
Q13.
package Exam;
public class Test {
public static void main(String[] args) {
String a = "stringy";
switch(a){
case "stringy": System.out.println("Strings work");
break;
case "string": System.out.println("Strings don't work");
}
}
}
What does the code print? Select one option.
A) Compile time error
B) “Strings work”
C) “Strings don’t work”
D) Runtime error
Q13 Answered.
Q14.
package Exam;
public class Test{
public static void main(String[] args) {
boolean a = true;
switch(a){
case true: System.out.println("Booleans work");
break;
case false: System.out.println("Booleans don’t work");
}
}
}
What does the code print? Select one option.
A) Compile time error
B) “Booleans work”
C) “Booleans don’t work”
D) Runtime error
Q14 Answered.
Q15.
package Exam;
public class Test extends Build{
public static void main(String[] args) {
Test q = new Test ();
StringBuilder sb = new StringBuilder("");
q.display(sb);
q.display();
}
private void display(StringBuilder ss){
ss.append("America".substring(4, 5));
ss.append("America".substring(1, 2));
ss.append("America".substring(6, 7));
System.out.println(ss);
}
}
class Build{
void display(){
StringBuilder ___ = new StringBuilder("");
___.append("Mexico".substring(0, 2));
System.out.println(___);
}
}
What is the output of the code? Select one answer?
A) Compile time error
B) “Me” followed by “Me”
C) “ima” followed by “Me”
D) “ima” followed by “ima”
Q15 Answered.
Q16.
package Exam;
public class Test extends TestSuper {
public static void main(String[] args) {
Test qq = new Test();
qq.out();
}
}
class TestSuper {
protected void out(){
System.out.println("1 out");
}
}
What does the code print? Select one answer.
A) Does not compile
B) “2 out”
C) “1 out”
D) Runtime error
Q16 Answered.
Q17.
List ls = new ArrayList(); //line 1
ArrayList ls = new ArrayList(); //line2
Which of the following is generally a better coding standard?
A) line 1
B) line 2
Q17 Answered.
Q18.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
q.go(5, "this", "that");
q.go(6, "this");
}
public void go(int i, String...k){
for(String l:k){
System.out.print(l);
}
}
public void go(int i, String k){
System.out.println(k + " done");
}
}
What does the code print? Select one option.
A) Compile time error
B) “thisthat” followed by “this”
C) “5thisthat” followed by “6this”
D) “thisthat” followed by “this done”
Q18 Answered.
Q19.
package Exam;
import java.util.ArrayList;
public class ArrListTest {
public static ArrayList arrLst = new ArrayList();
public static void main(String[] args) {
populateArr();
}
public static void populateArr(){
for(int i = 1; i <= 10; i++){
arrLst.add(i);
}
for (int x = 0; x<=9; ++x){
System.out.println(arrLst.get(x));
}
}
}
What will the code print? Select 1 answer.
A) The numbers 1 through 10
B) The numbers 1 through 9
C) The numbers 2 through 10
D) It will not compile.
Q19 Answered.
Q20.
package Exam;
public class Test {
public static void main(String[] args) {
char c = 'a'; //line 1
int x = c; //line 2
double y = x; //line 3
short z = c; //line 4
char cc = z; //line 5
}
}
Which of the lines of code do not compile? There may be multiple answers.
A) line 1
B) line 2
C) line 3
D) line 4
E) line 5
Q20 Answered.
Q21.
package Exam;
public class Test extends Abs { //Line 1
public static void main(String[] args) {
}
}
abstract class Abs implements Ione, Itwo{ //Line 2
}
interface Ione{ //Line 3
public void doThis();
}
interface Itwo{ //Line 4
public void doThat();
}
Which of the lines of code do not compile? Select one answer.
A) line 1
B) line 2
C) line 3
D) line 4
Q21 Answered.
Q22.
package Exam;
public class Test {
static boolean b;
public static void main(String[] args) {
int integ = 0;
if(b){
integ=2;
}
for(int i = 0; i < integ; i+=2, integ+=1.5){
System.out.print(integ);
}
}
}
What is the output of the code? Select one answer
A) Compile time error
B) “23”
C) “”
D) “24”
E) An infinite loop is entered with output “”
Q22 Answered.
Q23.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test(); //Line 1
StringBuilder z = q.runThis(); //Line 2
System.out.println(z);
}
StringBuilder public runThis(){ //Line 3
StringBuilder x = new StringBuilder("abc");
StringBuilder y = new StringBuilder("xyz");
StringBuilder z = new StringBuilder("");
for(int i = 0; i < x.length(); i++){
z.append(x.charAt(i)); //Line 4
z.append(y.charAt(i));
}
return z;
}
}
Which of the lines of code do not compile? Select one answer.
A) line 1
B) line 2
C) line 3
D) line 4
E) There is no problem and the code output is “axbycz”
Q23 Answered.
Q24.
package Exam;
public class Test{
static boolean a;
static boolean b = true;
static boolean c = false;
public static void main(String[] args) {
if(a == c != b){
System.out.print("One is true");
}else{
System.out.print("One is false");
}
}
}
What is the output of the code? Select one answer.
A) “One is true”
B) “One is false”
Q24 Answered.
Q25.
package Exam;
public class True{
static boolean a = true;
static boolean b = true;
static boolean c = false;
public static void main(String[] args) {
if(a = c != b){
System.out.print("Two is true");
}else{
System.out.print("Two is false");
}
}
}
What is the output of the code? Select one answer.
A) “Two is true”
B) “Two is false”
Q25 Answered.
Q26.
Which of the following is not a type of inner class?
A) static member class
B) static local class
C) non-static member class
D) non static local class
E) non-static anonymous class
Q26 Answered.
Q27.
package Exam;
public class Test{
public static void main(String[] args) {
char c = '10';
System.out.println(c);
}
}
True or false: The code prints “10”.
A) True
B) False
Q27 Answered.
Q28.
Which of the following is not a reason to use nested classes?
A) It is a way to coherently group classes used in only one part of the code
B) It cuts down on the total number of classes used in the code
C) It increases encapsulation
D) It often leads to more readable and maintainable code
Q28 Answered.
Q29.
package Exam;
public class Test {
int side1;
int side2;
private static String name;
public static void main(String[] args) {
Test q = new Test ("Rectangle",4,5);
whatName(q);
}
static void whatName(Test q){
System.out.println(name + ". Area is " + q.side1*q.side2);
}
public Test (String shape, int one, int two){
side1 = one;
side2 = two;
name = shape;
}
}
What does the code print? Select one option.
A) “null. Area is 20”
B) “Rectangle. Area is ”
C) “Rectangle. Area is 20”
D) Does not compile
Q29 Answered.
Q30.
What is the proper way to instantiate a static nested object?
A) OuterClass.StaticNestedClass nestedInstance = new
OuterClass.StaticNestedClass();
B) StaticNestedClass nestedInstance = new StaticNestedClass();
Q30 Answered.
Q31.
Which of the following is not an example of inheritance? Select one option.
A) An object has a field that its superclass has
B) An object has a method that its superclass has
C) An object has a method that its interface has
D) An object has a static field that its superclass has
Q31 Answered.
Q32.
True or false? In Java, an object can have only one subclass but unlimited
superclasses.
A) True
B) False
Q32 Answered.
Q33.
True or false? An object can implement multiple interfaces.
A) True
B) False
Q33 Answered.
Q34.
package Exam;
public class Test {
public static void main(String[] args) {
byte x = 100;
byte y = 100;
byte z = x+y;
System.out.println(z);
}
}
What does the code print? Select one option.
A) “200”
B) “-56”
C) “-55”
D) “-54”
E) Does not compile
Q34 Answered.
Q35.
True or False? A String is a primitive data type.
A) True
B) False
Q35 Answered.
Q36.
package Exam;
public class Test {
String x;
public static void main(String[] args) {
Test q = new Test();
if(q.x != null & !q.x.equals("")){
System.out.println("x is " + q.x);
}else{
System.out.println("No value for x");
}
}
}
What does the code print? Select one option.
A) “x is x”
B) “No value for x”
C) Does not compile
D) Runtime Exception
Q36 Answered.
Q37.
String str = " this".concat("that ").substring(3).trim().valueOf(2);
What is the value of str after the above code executes?
A) 2
B) s
C) t
D) Does not compile
Q37 Answered.
Q38.
package Exam;
public class Car implements Vehicle{
int engine = 1; //Line 1
int speed = 5;
String color = "Silver";
public static void main(String[] args) { //Line 2
}
public int getEngineSize(){ //Line 3
return engine;
}
public int getSpeed(){ //Line 4
return speed;
}
public String getPaint(){
return color;
}
}
interface Vehicle{ //Line 5
double getEngineSize();
int getSpeed();
String getPaint();
}
Which line of code causes the compile time issue? Select one option.
A) Line 1
B) Line 2
C) Line 3
D) Line 4
E) Line 5
F) The code compiles as-is
Q38 Answered.
Q39.
Which of the following are valid modifiers of class? Select four options.
A) private
B) final
C) abstract
D) public
E) default
Q39 Answered.
Q40.
Which of the following is not a reserved word in java? Select one option.
A) abstract
B) compile
C) do
D) enum
E) goto
Q40 Answered.
Q41.
Package Exam;
public class Tester{
private static int classX = 10;
public static void main(String []args){
Tester test = new Tester();
classX = 11;
System.out.println(test.accessClassX());
}
public int accessClassX () {
return ++classX;
}
}
What does the code print? Select one option.
A) “10”
B) “11”
C) “12”
D) Does not compile
Q41 Answered.
Q42.
Package Exam;
public class Tester{
private int myInt = 5;
public static void main(String []args){
Tester test = new Tester();
test.myInt = 6;
System.out.println(test.incrementIntAgain(test.incrementInt(test.myInt)));
}
public int incrementInt (int theirInt) {
return theirInt++;
}
public int incrementIntAgain (int theirInt) {
return ++theirInt;
}
}
What is the output of this code?
A) 5
B) 6
C) 7
D) 8
Q42 Answered.
Q43.
True or False: an instance level object can access a static variable?
A) True
B) False
Q43 Answered.
Q44.
True or False? An ArrayList can store primitives.
A) True
B) False
Q44 Answered.
Q45.
Which of the following is not a Collection interface method?
A) boolean isEmpty()
B) int size()
C) List getData()
D) boolean contains(Object element)
Q45 Answered.
Q46.
package Exam;
import java.util.function.Predicate;
public class PredicateTest{
Predicate<String> containsS = testString -> (testString.contains("money"));
public static void main(String []args){
System.out.println(containsS.test("bank"));
}
}
What does the code print?
A) true
B) false
C) Does not compile
Q46 Answered.
Q47.
package Exam;
import java.util.function.Predicate;
public class PredicateTest{
public static void main(String []args){
Predicate<Integer> greaterThan = intValue -> (intValue > 0);
Predicate<Integer> lessThan = intValue -> (intValue < 100);
System.out.println(greaterThan.or(lessThan).test(-5));
}
}
What will the code print?
A) true
B) false
C) Does not compile
Q47 Answered.
Q48.
package Exam;
public class AnonymousTest{
public static void main(String []args){
System.out.println(new String("5"));
}
}
True or False? The following code does not compile.
A) True
B) False
Q48 Answered.
Q49.
package Exam;
public class ConversionTest{
public static void main(String []args){
int x = 10;
double y = 10.5;
System.out.println(x + y);
}
}
What does the code print?
A) 20
B) 20.5
C) Does not compile
Q49 Answered.
Q50.
True or False? A do-while loop always executes at least once.
A) True
B) False
Q50 Answered.
Test 4 Questions
Q1.
package Exam;
public class Test {
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.methodM());
}
public String methodM(){
if(4<5){
return "First return";
}else{
return "Second return";
}
return "Last Return";
}
}
Which of the following is the output of the code? Select one option.
A) “First return”
B) “Second return”
C) “Last return”
D) Does not compile
Q1 Answered.
Q2.
package Exam;
public class Test {
public static void main(String[] args) {
String financial = spends() ? "poor":"rich";
System.out.println(financial);
}
public static boolean spends(){
return false;
}
}
What is the output of the code? Select one option.
A) Does not compile
B) “rich”
C) “poor”
Q2 Answered.
Q3.
package Exam;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.InputException;
public class Test {
public static void main(String[] args) {
BufferedReader reader = null;
try {
File file = new File("passTheTest.txt");
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (InputException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (InputException e) {
e.printStackTrace();
}
}
}
}
What issue is there with this code? Select one answer.
A) BufferedReader is not a real class
B) passTheTest.txt cannot be read by the File class
C) InputException does not need to be imported because it is in package
java.lang
D) InputException does not exist
Q3 Answered.
Q4.
package Exam;
public class Test {
public static void main(String[] args) {
int r = 0;
do{
System.out.println("r equals " + r+2);
}while((r+=2) < 6);
}
}
What is the last line of output of the code? Select one option.
A) “r equals 62”
B) “r equals 8”
C) “r equals 42”
D) “r equals 4”
E) “r equals 22”
F) “r equals 2”
Q4 Answered.
Q5.
package Exam;
public class Test {
public static void main(String[] args) {
int r = 0;
if(r+2 < 2){
System.out.println("Print this");
}
if(r+=2 < 3){
System.out.println("Print that");
}
}
}
What is the output of the code? Select one option.
A) Compile time error.
B) “Print this”
C) “Print that”
D) Run time error
Q5 Answered.
Q6.
package Exam;
public class Test {
public static void main(String[] args) {
double[] d = {1,2};
ArrayList l = aa(d, 3);
for(int i = 0; i < l.size(); i++){
System.out.print(l.get(i) + " ");
}
}
public static ArrayList aa(double[] a, double s) {
ArrayList<Double> arr = new ArrayList<Double>();
double nd = 0;
for (int i = 0; i < a.size(); i++) {
nd = s * a.get(i);
arr.add(nd);
}
return arr;
}
}
What is the output of the code? Select one option.
A) “3.0 6.0”
B) “3 6”
C) “2 4”
D) Does not compile
Q6 Answered.
Q7.
The native modifier is only valid for methods, not for variables or
constructors.
True or False?
A) True
B) False
Q7 Answered.
Q8.
Which of the following are valid variable modifiers? Select one option.
A) public, native, final
B) transient, volatile, native
C) synchronized, final, static
D) final, transient, volatile, static
Q8 Answered.
Q9.
Which of the following are all valid modifiers of methods? Select one
option.
A) final, abstract, synchronized, native
B) abstract, volatile, static
C) transient, static, public, synchronized
D) abstract, transient, public
Q9 Answered.
Q10.
package Exam;
public class Test {
public static void main(String[] args) {
loop:
for(int i = 0; i < 10; i++){
System.out.println(i);
if(i == 5){
break loop;
}else if((i%2) > 0){
continue;
}else{
i++;
}
}
}
}
What is the last line of output? Select one option.
A) “5”
B) “4”
C) “10”
D) “8”
E) Compile time error
Q10 Answered.
Q11.
package Exam;
public class Test {
public static void main(String[] args) {
loopOuter:
for(int i = 0; i < 10; i++){
System.out.println(i);
while(i%5 > 0){
i++;
if(i > 3){
break;
}
continue loopOuter;
}
}
}
}
What is the output of the last line of code? Select one option.
A) “10”
B) “8”
C) “9”
D) “7”
Q11 Answered.
Q12.
package Exam;
public class Test {
public static void main(String[] args) {
Test q1 = new Test();
Test q2 = new Test();
if(q1 instanceof q2){
System.out.println("Will this print?");
}else{
System.out.println("No");
}
}
}
What is the output of the code? Select one option.
A) “Will this print”
B) Compile time error
C) “No”
D) Run time error
Q12 Answered.
Q13.
package Exam;
public class Test {
public static void main(String[] args) {
int i = 5;
float f = 5;
if(i == f){
double d = 5;
if(f == d){
System.out.println("Java promotes float to double");
}else{
System.out.println("Java will not promote float to double");
}
}else{
System.out.println("Java will not promote int to float");
}
}
}
What is final line of output of the code? Select one option.
A) “Java promotes float to double”
B) “Java will not promote float to double”
C) “Java will not promote int to float”
D) There is no output
Q13 Answered.
Q14.
package Exam;
public class Test {
public static void main(String[] args) {
double d = 2.1;
float f = 4.131f;
if(d == 2){
System.out.println(d);
}
else if((int) f == 4){
System.out.println(f);
}
else{
System.out.println("Neither is equal");
}
}
}
What is the output of the code? Select one option.
A) “2.1”
B) “4.131”
C) “2”
D) “4”
E) “Neither is equal”
Q14 Answered.
Q15.
package Exam;
public class Test {
public static void main(String[] args) {
float f = 5.2f;
double r = 7.2;
Test q = new Test();
q.runInt(r);
q.runInt(f);
}
public void runInt(double r){
Integer i = new Integer(new Double(r).toString());
System.out.println(i + " ");
i = new Integer(new Double(r).intValue()*(int)3.2);
System.out.print(i + " ");
}
public void runInt(float f){
Integer i = new Integer(new Float(f).intValue() * (int) 1.1f);
System.out.print(i + " ");
}
}
What is the output of the code? Select one option.
A) “7 21 5”
B) “7.2 23.04 5.72”
C) Compile time exception
D) Runtime exception
Q15 Answered.
Q16.
Given the following code:
package Exam;
public class Test {
public static void main(String[] args) {
int r = 5;
TC tc = new TC();
tc.tryThis(r);
System.out.println(r);
}
}
class TC {
public void tryThis(int i){
int x;
i += 1;
System.out.println(i+x);
}
}
What will be the output? Select 1 option.
A) “6” followed by “6”
B) “6” followed by “5”
C) “null” followed by “5”
D) “5” followed by “6”
E) Does not compile
Q16 Answered.
Q17.
package Exam;
public class Test {
public static void main(String[] args) {
Orange o = new Orange("Big Orange", 2);
System.out.println(o.getName() + “ “ + o.getDiameter());
}
}
class Orange{
private int diameter = 1;
private String name;
public void setName(String n){
this.name = n;
}
public String getName(){
return this.name;
}
public void setDiameter(int d){
this.diameter = d;
}
public int getDiameter(){
return this.diameter;
}
public Orange(String s, int d){
setName(s);
setDiameter(d);
}
public Orange(int d){
setName("Orange");
setDiameter(d);
}
}
What is the output of the code? Select one option.
A) “Orange 2”
B) “Big Orange 2”
C) “Orange 1”
D) “Big Orange 1”
Q17 Answered.
Q18.
package Exam;
public class Test {
public static void main(String[] args) {
outer:
for(int i = 3; i< 7; i++){
for(int ii = i/2; ii < i; ii += 2){
if(ii > i*2){
continue outer;
}
else{
System.out.print(ii + " ");
break;
}
}
}
}
}
What is the output of the code? Select one option.
A) “1 2 2”
B) “1 2”
C) “0 1 2 3”
D) “1 2 2 3”
Q18 Answered.
Q19.
package Exam;
public class Test {
public static void main(String[] args) {
int i = 0;
int counter = 0;
while (i < 3){
counter++;
if("Hello world" == "Hello " + "world"){
i += 3%2;
}else{
counter+=i;
i++;
}
}
System.out.println(counter);
}
}
What is the output of the code? Select one option.
A) “3”
B) “5”
C) It enters an infinite loop
D) “6”
Q19 Answered.
Q20.
How can a programmer make a field in a class invisible to all other classes?
There may be more than one answer.
A) Give the class the private modifier
B) Give the field the private modifier
C) No modifier is needed. By default, all fields and methods are private
until given a different modifier
D) Give the class the protected modifier
E) Give the field the protected modifier
Q20 Answered.
Q21.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
StringBuilder z = q.runThis();
System.out.println(z);
}
public runThis(){
StringBuilder x = new StringBuilder("abcdef");
StringBuilder z = new StringBuilder("");
for(int i = 0; i < x.length(); i++){
z.append(x.charAt(i));
z.append(x.charAt(i+3));
}
return z;
}
}
What is the output of the code? Select one option.
A) Does not compile
B) “abcdef”
C) “adbecf”
Q21 Answered.
Q22.
package Exam;
public class Test{
public static void main(String[] args) {
Test q = new Test();
q.runThis();
}
public void runThis(){
StringBuilder x = new StringBuilder("abcdef");
StringBuilder z = new StringBuilder("");
boolean a;
for(int i = 0; i < x.length(); i++){
z.append(x.charAt(i));
if(a = true == true == false){
z.append(x.charAt(i+3));
}
}
System.out.println(z);
return;
}
}
What is the output of the code? Select one option.
A) Does not compile
B) “abcdef”
C) “adbecf”
D) prints nothing
Q22 Answered.
Q23.
package Exam;
public class Test extends Build{
public static void main(String[] args) {
Test q = new Test();
q.display();
}
private void display(){
StringBuilder $1 = new StringBuilder("");
$1.append("Hello World".substring(2, 3));
$1.append("Hello World".substring(7, 9));
$1.append("Hello World".substring(10, 11));
System.out.println($1);
}
}

class Build{
private void display(){
StringBuilder $1 = new StringBuilder("");
$1.append("Hello World".substring(0, 1));
$1.append("Hello World".substring(7, 1));
$1.append("Hello World".substring(9, 1));
$1.append("Hello World".substring(10, 1));
System.out.println($1);
}
}
What is the output of the code? Select one option.
A) Compile time error
B) “Hold”
C) Runtime error
D) “lord”
Q23 Answered.
Q24.
package Exam;
public class Test extends Build{
public static void main(String[] args) {
Test q = new Test();
q.display();
}
private void display(){
StringBuilder $1 = new StringBuilder("");
$1.append("ChaChaCha".substring(0, 1));
$1.append("ChaChaCha ".substring(3, 4));
$1.append("ChaChaCha ".substring(6, 7));
System.out.println($1);
}
}
class Build{
void display(){
StringBuilder $1 = new StringBuilder("");
$1.append("ChaChaCha ".substring(2,3));
$1.append("ChaChaCha ".substring(7, 8));
$1.append("ChaChaCha ".substring(10, 11));
System.out.println($1);
}
}
What is the output of the code? Select one option.
A) Compile time error
B) “CCC”
C) Runtime error
D) “aaa”
Q24 Answered.
Q25.
package Exam;
public class Test {
public static void main(String[] args) {
String num = "55";
int r = new Integer(num).intValue(); //line 1
double x = Double.valueOf(r); //line 2
r = x; //line 3
}
}
Which line causes a compile time error? Select one option.
A) line 1
B) line 2
C) line 3
D) none
Q25 Answered.
Q26.
package Exam;
public class Test{
public static void main(String[] args) {
switch(args[0].charAt(0)){
case TestConstants.option1: System.out.println("Option 1
Selected");
case TestConstants.option2: System.out.println("Option 2
Selected");
case TestConstants.option3: System.out.println("Option 3
Selected");
}
}
}
class TestConstants{
public static final String option1 = “A”;
public static final String option2 = “B”;
public static final String option3 = “C”;
}
What does the following code print when run from the command line with
the command java Test C? Select one of the following.
A) “Option 1 Selected”
B) “Option 2 Selected”
C) “Option 3 Selected”
D) Does not compile
Q26 Answered.
Q27.
package Exam;
public class Test extends SuperTest{
public static void main(String[] args) {
SuperTest q = new Test();
System.out.println(q.s);
}
public void run(){
s="Sub";
}
}
class SuperTest{
String s;
SuperTest(){
run();
}
public void run(){
s = "Super";
}
}
What does the following code print when run from the command line with
the command java Test C? Select one of the following.
A) “Super”
B) “Sub”
C) Does not compile
Q27 Answered.
Q28.
What is the code for instantiating an anonymous inner class? Select one of
the following.
A) String s = new String(“example”);
B) String s = “example”;
C) new String(“example”);
Q28 Answered.
Q29.
package Exam;
public class Test {
public static void main(String[] args) {
int[] numbers = {1,2,3,4,5};
for (int numies : numbers)
{
System.out.print(numies + “, “);
}
}
}
What is the output of the code? Select one option
A) “1, 2, 3, 4, 5, “
B) “5, 4, 3, 2, 1, “
C) Does not compile
Q29 Answered.
Q30.
package Exam;
public class Test {
static int i = 0;
static short s = 0;
static byte b = 0;
static double d = 0;
static char c = '0';
public static void main(String[] args) {
s = d; //Line 1
i = c; //Line 2
c = (char) i; //Line 3
i = (int) d; //Line 4
}
}
Which lines compile properly? Select 3.
A) Line 1
B) Line 2
C) Line 3
D) Line 4
Q30 Answered.
Q31.
True or false? An interface can extend an object.
A) True
B) False
Q31 Answered.
Q32.
True or false? An object inherits all the fields and methods of its superclass.
A) True
B) False
Q32 Answered.
Q33.
Which of the following are valid modifiers of interface methods? Select two.
A) public
B) protected
C) private
D) abstract
Q33 Answered.
Q34.
package Exam;
public class Test{
public static void main(String[] args) {
Race m = new Marathon();
System.out.println(m.getName());
Race t = new Triathalon();
System.out.println(t.getName());
}
}
class Marathon implements Race{
String name = "marathon";
double distance = 10;
public double getDistance(){
return distance;
}
public String getName(){
return name;
}
}
class Triathalon implements Race{
String name = “triathalon”;
public double getDistance(){
return distance;
}
public String getName(){
return Race.name;
}
}
interface Race{
double distance = 26.2;
String name = "race";
String getName();
double getDistance();
}
What is the output of the code? Select 1 option.
A) “marathon” followed by “race”
B) “race” followed by “race”
C) “marathon” followed by “triathalon”
D) Does not compile
Q34 Answered.
Q35.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
try{
q.run();
}catch(ExceptionA x){
System.out.println("Caught it!");
}
}
public void run() throws ExceptionB{
throw new ExceptionB();
}
}
class ExceptionA extends Exception{}
class ExceptionB extends ExceptionA{}
True or false? The code prints “Caught it!”.
A) True
B) False
Q35 Answered.
Q36.
package Exam;
public class Armadillo extends Critter {
public static void main(String[] args) {
Critter q = new Armadillo();
System.out.print(q.areTheyBig());
System.out.print(areTheyHairy());
}
public static String areTheyHairy(){
return "No";
}
public String areTheyBig(){
return "No ";
}
}
class Critter{
public static String areTheyHairy(){
return "Yes";
}
public String areTheyBig(){
return "Maybe ";
}
}
What does the code print? Select one answer.
A) “Maybe Yes”
B) “Maybe No”
C) “No Yes”
D) “No No”
Q36 Answered.
Q37.
What is the proper package and class name of checked exceptions? Select
one answer.
A) java.util.CheckedException
B) java.lang.RuntimeException
C) java.lang.Exception
D) java.util.Exception
E) java.lang.CheckedException
Q37 Answered.
Q38.
package Exam;
import java.util.ArrayList;
public class Test{
public static void main(String[] args) {
System.out.println(arrayOops());
}
public static String arrayOops(){
ArrayList arr = new ArrayList();
try{
System.out.println(arr.get(0));
}catch(RuntimeException e){
return "catch block";
}finally{
return "finally block";
}
}
}
What does the code print? Select one answer.
A) “catch block”
B) “finally block”
C) Compile time error
Q38 Answered.
Q39.
Which modifiers allow access to a method or field by all other classes in the
same package? Select three answers.
A) public
B) protected
C) no modifier (package-protected)
D) private
Q39 Answered.
Q40.
Which of the following are generally true regarding access modifiers?
A) Fields should have the most narrow access modifier possible
B) Methods should have the widest access modifier possible
C) Avoid using public fields unless the field is a constant
Q40 Answered.
Q41.
True or False: It is necessary to use the @FunctionalInterface annotation on
functional interfaces?
A) True
B) False
Q41 Answered.
Q42.
package Exam;
interface TestInterface {
public void add(int x);
default void print() {
System.out.println("Default Method Executed");
}
default void printAnother() {
System.out.println("Another Default Method Executed");
}
}
public class Test implements TestInterface {
public void add(int x) {
System.out.println(x+x);
}
public static void main(String args[]) {
Test writer = new Test();
writer.print();
}
}
Which of the following is the output of the code? Select one option.
A) “Default Method Executed”
B) “Another Default Method Executed”
C) Compile time error
Q42 Answered.
Q43.
What class does ArrayList extend?
A) ArrayList does not extend any classes
B) AbstractList
C) Array
D) ListCollection
Q43 Answered.
Q44.
package Exam;
public class CaseTest{
private String justInCase(String str){
String returner = "";
switch(str) {
case "a":
returner.concat("a");
case "b":
returner.concat("b");
default:
returner.concat("done");
}
return returner;
}
public static void main(String []args){
CaseTest ct = new CaseTest();
System.out.println(ct.justInCase("a"));
}
}
What does the code return?
A) “a”
B) “abdone”
C) “done”
D) an empty string
Q44 Answered.
Q45.
True or False? A try statement must always be followed by at least one catch
statement
A) True
B) False
Q45 Answered.
Q46.
True or False? Local classes are classes defined in a block of code. Local
classes are typically defined inside a method.
A) True
B) False
Q46 Answered.
Q47.
Which of the following cannot be anonymous? Select all that apply.
A) Classes
B) Methods
C) Interfaces
D) Variables
Q47 Answered.
Q48.
package Exam;
public class Test {
public static void main(String[] args) {
Integer[][] arr = {{0, 3, 12},{2, (Integer) null}};
System.out.println(arr[1][1]);
}
}
True or False? The (Integer) casting is required for the code to compile?
A) True
B) False
Q48 Answered.
Q49.
True or False? The “super” keyword cannot be used in any method except a
constructor.
A) True
B) False
Q49 Answered.
Q50.
package Exam;
public class Test {
public static void main(String[] args) {
loop:
for(int i = 10; i > 0; i--){
System.out.println(i);
if(i == 3){
break loop;
}else if((i%3) > 0){
continue;
}else{
i-=2;
}
}
}
}
What is the last line printed by the code?
A) 3
B) 1
C) 6
D) 10
Q50 Answered.
Test 5 Questions
Q1.
package Exam;
public class Test extends SuperTest {
public static void main(String[] args) {
System.out.println("This is all there is");
}
}
class SuperTest {
int x = 0;
String y = "super";
char z = 'z';
static {System.out.println("super print"); }
SuperTest (int x){
this.x = x;
}
SuperTest (String y){
this.y = y;
}
SuperTest (char z){
this.z = z;
}
}
Which of the following is the output of the code? Select one option.
A) “super print” followed by “This is all there is”
B) “This is all there is” followed by “super print”
C) “This is all there is”
D) Compile time error
Q1 Answered.
Q2.
package Exam;
public class Test {
public static void main(String[] args) {
int x = 1;
switch (x){
case 1: System.out.print("At 1 ");
x = 3;
case 2: System.out.print("At 2 ");
x = 1;
case default: System.out.print("At default ");
}
}
}
Which of the following is the output of the code? Select one option.
A) “At 1 At default”
B) Compile time error
C) “At 1”
D) “At 1 At 2 At default”
Q2 Answered.
Q3.
package Exam;
public class Test {
public static void main(String[] args) {
System.out.print("Main method + ");
System.out.print(new Test().hypotenuse());
}
Test(){
int length = 3;
int height = 4;
}
public double hypotenuse(){
return Math.sqrt(3*3+4*4);
}
}
Which of the following is the output of the code? Select one option.
A) “Main method + “
B) “Main method + 5.0”
C) “Main method + 5”
D) “Main method + 52”
E) “Main method + 52.0”
F) Compile time error
Q3 Answered.
Q4.
package Exam;
public class Test {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
Test q = new Test(sb);
if(sb.toString().equals(this.toString())){
System.out.print("True");
}
}
Test(StringBuilder stringB){
stringB.append(this.toString());
}
}
True or False: The code will print “True”.
A) True
B) False
Q4 Answered.
Q5.
package Exam;
public class Test {
public static void main(String[] args) {
char c = "a";
String s = "a";
if(c.equals(s)){
System.out.println("True");
}
}
True or false: The code will print “True”.
A) True
B) False
Q5 Answered.
Q6.
package Exam;
public class Test {
public static void main(String[] args) {
System.out.println(args[1]);
}
}
The app is run from the command line with the following command:
java Test Print this code
Which of the following is the output of the code? Select one option.
A) Runtime Exception
B) “Test”
C) “Print”
D) “this”
E) “code”
F) “Print this code”
Q6 Answered.
Q7.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
try{
ArrayList arr = new ArrayList();
arr.add("Rugby");
System.out.println(arr.get(1));
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Caught ArrayIndexOutOfBoundsException");
}catch(Exception e){
System.out.println("Caught Exception");
}
}
}
Which of the following is the output of the code? Select one option.
A) “Rugby”
B) “Caught ArrayIndexOutOfBoundsException”
C) “Caught Exception”
D) Runtime error
Q7 Answered.
Q8.
package Exam;
public class Test {
public static void main(String[] args) {
try{
try{
ArrayList arr = new ArrayList();
arr.add("Rugby");
System.out.println(arr.get(1));
}catch(IndexOutOfBoundsException e){
throw new ArrayIndexOutOfBoundsException();
}catch(Exception e){
System.out.println("Caught Inner Exception");
}
}catch(Exception e){
System.out.println("Caught Outer Exception");
}
}
}
Which of the following is the output of the code? Select one option.
A) “Caught Inner Exception”
B) “Caught Outer Exception”
C) Runtime exception
Q8 Answered.
Q9.
package Exam;
public class Test {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
The app is run from the command line with the following command:
java Test “Print more code”
Which of the following is the output of the code? Select one option.
A) “Test”
B) “Print”
C) “more”
D) “Print more code”
E) Runtime Exception
Q9 Answered.
Q10.
package Exam;
public class Test {
public static void main(String[] args) {
if("This string" == new String("This string")){
System.out.println("This string".reverse());
}else{
System.out.println("This string".length());
}
}
}
Which of the following is the output of the code? Select one option.
A) “gnirts sihT”
B) “11”
C) “10”
D) Compile time error
Q10 Answered.
Q11.
package Exam;
public class Test {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
arr.add(1);
arr.add(2);
arr.add(3);
try{
//Line 1
}catch(Exception e){
System.out.println(e);
}
}
}
Which of the following can be placed at Line 1? Select two options.
A) System.out.println(arr.get(2));
B) System.out.println(arr[2]);
C) System.out.println(arr.get(5));

Q11 Answered.
Q12.
package Exam;
public class Test {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
if(a = b){
System.out.println("First if");
}else if(b = a){
System.out.println("Second if");
}else if(b = !a){
System.out.println("Third if");
}else if(a == !b){
System.out.println("Fourth if");
}
}
}
Which of the following is the output of the code? Select one option.
A) “First if”
B) “Second if”
C) “Third if”
D) “Fourth if”
Q12 Answered.
Q13.
Which of the following are object oriented concepts? Select three answers.
A) Designing classes to be modular and easily reusable
B) Storing object states in fields
C) Experiencing object behavior through methods
D) Having code flow in a linear fashion in an application
Q13 Answered.
Q14.
Which of the following code snippets compile? Select two options.
A) for(false){}
B) if(false){}
C) while(false){}
D) Boolean bool = false; while(bool){}
The answers are B, D. for() requires a value to iterate through. while() is
evaluated more strictly than if() by the JVM, so the JVM evaluates
while(false) and sees that the code inside the while statement is unreachable.
if(false) is also unreachable, but the JVM allows that code to compile.
Q14 Answered.
Q15.
package Exam;
public class Test {
int size = 1;
String shape = "none";
public void Test(){
size = 10;
shape = "round";
}
public int getSize(){
return this.size;
}
public String getShape(){
return this.shape;
}
}
class Tester{
public static void main(String[] args) {
Test q = new Test();
System.out.print( q.getSize() + " " + q.getShape());
}
}
Which of the following is the output of the code when Tester is run? Select
one option.
A) “10 round”
B) “10 none”
C) “1 round”
D) “1 none”
Q15 Answered.
Q16.
package Exam;
public class Test {
public static void main(String[] args) {
Integer[][] arr = {{0, 3, 12},{2, (Integer) null}};
System.out.println(arr[1][1]);
}
}
Which of the following is the output of the code? Select one option.
A) Runtime Exception
B) “0”
C) “null”
D) “3”
Q16 Answered.
Q17.

Which of the following are true?


A) A class cannot contain a static reference to a non-static variable and
compile.
B) All objects referenced in a class need to have an explicitly stated import
statement
C) If there is no package statement in a class, the JVM will throw a compile
time error
D) A class can be executed by the JVM even if no parameters are passed to
the class.
Q17 Answered.
Q18.
What are some of the benefits of object oriented programing? Select three
answers
A) Debugging is easier
B) Code is easily reusable
C) The code flows in a logical fashion
D) Code changes to one module do not affect another module
Q18 Answered.
Q19.
package Exam;
public class Test {
public static void main(String[] args) {
String s = "Team USA";
String y = "Team UK";
s.concat(y);
System.out.println(s);
modify(s,y);
System.out.println(s);
s=modify(s, y);
System.out.println(s);
}
public static String modify(String one, String two){
String mod = "";
mod = two.concat(" and") + one.substring(4);
return mod;
}
}
Which of the following is the output of the code? Select one option.
A) “Team USA” followed by “Team USA” followed by “Team UK and
Team USA”
B) “Team USA and Team UK” followed by “Team USA and Team UK”
followed by “Team UK and Team USA”
C) “Team USA” followed by “Team USA and Team UK ” followed by
“Team UK and Team USA”
D) “Team USA and Team UK” followed by “Team USA ” followed by
“Team UK and Team USA”
Q19 Answered.
Q20.
package Exam;
public class Test extends AA {
public static void main(String[] args) {
Test q = new Test();
q.goGet();
q.goGetter();
q.print();
}
protected void goGet(){
System.out.println("Hey!");
}
private void goGetter(){
System.out.println("Got it!");
}
static public void print(){
System.out.println("Sub Howdy!");
}
}
class AA {
public static void print(){
System.out.println("Super Howdy!");
}
}
What does the following code print? Select one of the following.
A) “Hey!, Got it!”
B) “Super Howdy!, Hey!, Got it!”
C) “Hey!” followed by “Got it!” followed by “Sub Howdy!”
D) “Hey!” followed by “Got it!” followed by “Super Howdy!”
E) Does not compile
Q20 Answered.
Q21.
package Exam;
public class Test{
public static void main(String[] args) {
switch(args[0].charAt(0)){
case TestConstants.option1: System.out.println("Option 1
Selected");
case TestConstants.option2: System.out.println("Option 2
Selected");
case TestConstants.option3: System.out.println("Option 3
Selected");
}
}
}
class TestConstants{
public static final char option1 = ‘S’;
public static final char option2 = ‘T’;
public static final char option3 = ‘X’;
}
What does the following code print when run from the command line with
the command java Test S? Select one of the following.
A) “Option 1 Selected”
B) “Option 2 Selected”
C) “Option 3 Selected”
D) Does not compile
Q21 Answered.
Q22.
package Exam;
public class Test extends SuperTest{
public static void main(String[] args) {
Test q = new Test();
System.out.println(q.s);
}
public void run(){
s="Sub";
}
}
class SuperTest{
String s;
SuperTest (){
run();
}
public void run(){
s = "Super";
}
}
What does the following code print? Select one of the following.
A) “Super”
B) “Sub”
C) Does not compile
Q22 Answered.
Q23.
package Exam;
public class Test extends SuperTest{
public static void main(String[] args) {
Test q = (Test) new SuperTest();
System.out.println(q.s);
}
public void run(){
s="This is called";
}
}
class SuperTest{
String s;
SuperTest(){
run();
}
public void run(){
s = "No, THIS is called";
}
}
What does the following code print? Select one of the following.
A) “This is called”
B) “No, THIS is called”
C) Does not compile
Q23 Answered.
Q24.
package Exam;
public class Test extends SuperTest {
public static void main(String[] args) {
Test q = new Test();
try {
q.run();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void run() throws FileNotFoundException{
System.out.println("Overridding methods can throw more specific
exceptions");
}
}
class SuperTest{
void run() throws Exception{
System.out.println("Overridden methods must throw more general
exceptions");
}
void run(int i) throws Exception{
System.out.println("This method is overloaded, not overridden");
}
}
What does the following code print? Select one of the following.
A) "Overriding methods can throw more specific exceptions”
B) “Overridden methods must throw broader exceptions”
C) “This method is overloaded, not overridden”
D) Does not compile
Q24 Answered.
Q25.
package Exam;
public class Test{
public static void main(String[] args) {
int i = 5;
while(--i>0){
System.out.println(i);
}
}
}
What is the first number printed to the console? Select one of the following.
A) “6”
B) “5”
C) “4”
D) “3”
Q25 Answered.
Q26.
package Exam;
public class Test{
public static void main(String[] args) {
float f = 5.0f;
Test q = new Test();
q.runInt(f);
}
public void runInt(float f){
Integer i = new Integer(new Float(f).intValue());
System.out.print(i );
}
}
What is the output of the code? Select one option.
A) “5”
B) “5.0”
C) Compile time exception
D) Runtime exception
Q26 Answered.
Q27.
package Exam;
public class Test {
public static void main(String[] args) {
int i = 5;
while((i+=2)<10){
System.out.println(i);
}
if(i > 10){
try {
throw new SubThrowable();
} catch (SubThrowable e) {
e.printStackTrace();
}
}
}
}
class SubThrowable extends Throwable{
SubThrowable(){
System.out.println("caught!!!");
}
}
True or false: the code compiles and prints “caught!!!” at some point.
A) True
B) False
Q27 Answered.
Q28.
package Exam;
public class Test extends SuperTest {
public static void main(String[] args) {
Test q = new Test();
q.returnThis();
}
public Integer returnThis(){
Integer integ = 10;
System.out.println(integ);
return integ;
}
}
class SuperTest{
public Number returnThis(){
Number num = 5;
return num;
}
}
What does the following code print? Select one of the following.
A) “10”
B) “5”
C) Does not compile
Q28 Answered.
Q29.
package Exam;
public class Test {
public static void main(String[] args) {
Long l = 5; //Line 1
for(;l<8; l++) //Line 2
{
System.out.println(l.toString()); //Line 3
}
}
}
Which line contains an error?
A) Line 1
B) Line 2
C) Line 3
D) No error
Q29 Answered.
Q30.
package Exam;
public class Test extends SuperTest{
static {System.out.println("Sub static block");} //Line 1
{System.out.println("Sub block");} //Line 2
public static void main(String[] args) {
Test q = new Test();
}
}
class SuperTest {
{System.out.println("Super block");} //Line 3
static {System.out.println("Super static block");} //Line 4
SuperTest(){
run1();
run2();
}
public void run1(){
{System.out.println("run1");} //Line 5
}
public static void run2(){
{System.out.println("run2");} //Line 6
}
}
In which order are the line numbers printed to the console? Select one of the
following.
A) 1, 3, 6, 2, 4, 5
B) 3, 4, 5, 6, 1, 2
C) 1, 2, 3, 4, 5, 6
D) 4, 1, 3, 5, 6, 2
E) 5, 2, 1, 3, 6, 4
Q30 Answered.
Q31.
True or false? A class implementing an interface must implement all
methods of the interface.
A) True
B) False
Q31 Answered.
Q32.
True or false? A package is a folder directory that is used for organizing
classes and interfaces.
A) True
B) False
Q32 Answered.
Q33.
package Exam;
public class Test {
public static void main(String[] args) {
Race t = new Triathlon();
System.out.println(t.getName());
System.out.println(t.getNew());
}
}
class Triathlon implements Race{
String name = "triathlon";
public double getDistance(){
return distance;
}
public String getName(){
return name;
}
public String getNew(){
return "Hello World";
}
}
interface Race{
double distance = 26.2;
String name = "race";
String getName();
double getDistance();
}
What does the code print? Select one of the following.
A) “triathlon” followed by “Hello World”
B) “race” followed by “Hello World”
C) Does not compile
Q33 Answered.
Q34.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
try{
q.run();
}catch(Exception e){
System.out.println("Caught it!");
}
}
public void run() throws ExceptionR{
try{
throw new ExceptionR();
}finally{
System.out.println("Finally block");
}
}
}
class ExceptionR extends Exception{}
What does the code print? Select one of the following.
A) “Caught it!”
B) “Finally block” followed by “Caught it!”
C) Does not compile
Q34 Answered.
Q35.
package Exam;
public class Num extends Small {
String s = "Very big";
final double number = 1000000000;
public static void main(String[] args) {
Small q = new Num();
System.out.println(q.s);
System.out.print(q.number);
}
}
class Small{
String s = "Very small";
final double number = .01;
}
What does the code print? Select one of the following.
A) “Very small” followed by “.01”
B) “Very big” followed by “.01”
C) “Very small” followed by “1000000000”
D) “Very big” followed by “1000000000”
Q35 Answered.
Q36.
What is the proper package and class name of unchecked exceptions? Select
one of the following.
A) java.lang.Exception
B) java.lang.RuntimeException
C) java.util.Exception
D) java.util.RuntimeException
Q36 Answered.
Q37.
package Exam;
import java.util.ArrayList;
public class Test{
public static void main(String[] args) {
try{
arrayOops();
System.out.println("No need to catch. ");
}catch(RuntimeException e){
System.out.println("RuntimeException is catchable. ");
}finally{
System.out.println("Was it caught? ");
}
}
public static void arrayOops(){
ArrayList arr = new ArrayList();
System.out.println(arr.get(0));
}
}
What does the code print? Select one of the following.
A) “Was it caught? ”
B) “No need to catch. Was it caught? “
C) “RuntimeException is catchable. Was it caught? “
D) Code throws a runtime exception
Q37 Answered.
Q38.
Which of the following modifiers allow all subclasses to access a method or
field? Select two of the following.
A) public
B) protected
C) no modifier (package-protected)
D) private
Q38 Answered.
Q39.
package Exam;
public class Test {
public static void main(String[] args) {
double r = Double.valueOf("6.7");
for(int x = 0; x < r; x+=Math.round(r)-4){
System.out.print(x + “ “);
}
}
}
What does the code print? Select one of the following.
A) 0 2 4 6
B) 0 3 6
C) 1 3 5
D) 1 4 7
E) Does not compile
Q39 Answered.
Q40.
package Exam;
import java.util.ArrayList;
import java.util.List;
public class 1Test {
List<String> dogs = new ArrayList<String>();
public static void main(String[] args) {
String dog1 = "German Shephard";
String dog2 = "Husky";
String dog3 = "Mutt";
Character dog4 = new Character('s');
1Test q = new 1Test();
q.dogs.add(dog1);
q.dogs.add(dog2);
q.dogs.add(dog3);
for(int i = 0; i<q.dogs.size(); i++){
System.out.print(q.dogs.get(i) + ",");
}
}
}
What will the output be from the above program? Select 1 answer.
A) “German Shephard,Husky, Mutt,”
B) “German Shephard,Husky, Mutt”
C) Compile time error.
D) None of the above
Q40 Answered.
Q41.
package Exam;
interface TestInterface {
default void print() {
System.out.println("Default method prints!");
}
}
public class Test implements TestInterface {
public void print() {
System.out.println(“This overrides!”);
}
public static void main(String args[]) {
Test writer = new Test();
writer.print();
}
}
Which of the following is the output of the code? Select one option.
A) “Default method prints!”
B) “This overrides!”
C) Compile time error
Q41 Answered.
Q42.
Which of the following interfaces is not directly or indirectly implemented by
ArrayList?
A) Array
B) List
C) Collection
D) Iterable
Q42 Answered.
Q43.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
try{
ArrayList arr = new ArrayList();
arr.add("Index?");
System.out.println(arr.get(1));
} finally {
System.out.println("Finally Here");
}
}
}
What does the code print?
A) “Index?”
B) “Finally Here”
C) “Index?” followed by “Finally Here”
D) “Finally Here” followed by exception stack trace
E) exception stack trace
Q43 Answered.
Q44.
package Exam;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<Integer, String> map = new Map<Integer, String>();
map.put(0, "element 0");
System.out.println(map.get(0));
}
}
What does the following code print?
A) “element 0”
B) null
C) Does not compile
Q44 Answered.
Q45.
Which of the following is not a concept of encapsulation?
A) Getters and Setters
B) java classes
C) extending classes
D) private modifier
Q45 Answered.
Q46.
package Exam;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
try{
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(0, "element 0");
map.put(2, "element 1");
System.out.println(map.get(1));
}catch(IndexOutOfBoundsException e){
System.out.println("Incorrect implementation.");
}
}
}
What does the code print?
A) “element 0”
B) “element 1”
C) “Incorrect implementation.”
D) null
Q46 Answered.
Q47.
package Exam;
public class Test extends Build{
public static void main(String[] args) {
Test q = new Test ();
StringBuilder sb = new StringBuilder("");
q.display(sb);
q.display();
}
private void display(StringBuilder ss){
ss.append("California".substring(2, 2));
ss.append("California".substring(1, 3));
ss.append("California".substring(4,6));
System.out.println(ss);
}
}
class Build{
void display(){
StringBuilder ___ = new StringBuilder("");
___.append("Texas".substring(0, 2));
System.out.println(___);
}
}
What does the code print?
A) “lalifor” followed by “Tex”
B) “alfo” followed by “Te”
C) “Caif” followed by “Te”
D) Does not compile
The answer is B.
Q47 Answered.
Q48.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
int counter = 0;
for(int i = 4; i > 0; i-=2){
for(int x = 0; x < i; ++x){
++counter;
}
}
System.out.println(counter);
}
}
What does the code print?
A) 2
B) 4
C) 6
D) 8
E) 10
The answer is 6. i-=2 is valid syntax
Q48 Answered.
Q49.
package Exam;
public class Tester extends Test {
public static void main(String[] args) {}
public void methodHide(){
System.out.println("method hidden");
};
}
abstract class Test extends TestAbstract {
public void methodRun(){
System.out.println("method ran");
}
}
abstract class TestAbstract {
abstract void methodRun();
abstract void methodHide();
}
True or False? The code compiles.
A) True
B) False
Q49 Answered.
Q50.
package Exam;
public class CaseTest{
private String justInCase(String str){
String returner = "";
switch(str) {
case "a":
returner += "a";
case "b":
returner += "b";
default:
returner += "done";
}
return returner;
}
public static void main(String []args){
CaseTest ct = new CaseTest();
System.out.println(ct.justInCase("a"));
}
}
What does the code return?
A) “a”
B) “b”
C) “done”
D) “abdone”
Q50 Answered.
Test 1 Answers
Q1 Answered.
package Exam;
public class Test extends TestAbstract {
public static void main(String[] args) {}
public void methodRun(){}
public void methodHide(){}
}
abstract class TestAbstract {
abstract void methodRun();
abstract void methodHide();
}
True or False: Test class needs an abstract modifier in order to compile.
A) True
B) False
The answer is False. The overridding methods in class Test are not
considered abstract because the methods have {}. Therefore it does not need
to be declared abstract.
Q1 Unanswered.
Q2 Answered.
package Exam;
public class ArrayCopy {
public static void main(String[] args) {
char[] copyFrom = {'c', 'r', 'u', 'n', 'c', 'h', 'y', ' ', 'c', 'o', 'o', 'k',
'i', 'e' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(copyTo);
}
}
What will the code print? Select 1 option.
A) “runchy”
B) “runchy c”
C) “unchy c”
D) “unchy co”
E) “crunchy”
The answer is C. Here is the signature of the relevant method:
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Q2 Unanswered.
Q3 Answered.
package Exam;
public class q1 {
public static void main(String[] args) {
int[][] a = new int[5][];
//Line 1
}
}
Which of the following could be placed at line 1 and the code would still
compile? Select 1 answer.
A) a[1] = new short[5];
B) a[1] = new int[5];
C) a[1] = new Integer[5];
D) a[1] = new double[5];
E) a[1] = new int[];
The answer is B. There is a type mismatch if any array type other than int[]
is assigned to a[1]. Also, the assignment to a[1] must be either dimension
expressions or an array initializer.
Q3 Unanswered.
Q4 Answered.
Given the following code:
package Exam;
public class TC {
static int i2[] = new int[1];
public static void main(String[] args) {
i2[1] = 0;
System.out.println(i2[].getClass());
}
}
What will be the output? Select 1 option.
A) “class java.lang.int”
B) Does not compile
C) Runtime exception
The answer is B. i2 is considered a primitive by the JVM for the getClass
method, since it is an array of ints. This is despite the fact that in Java, an
array is an object. The superclass Object has a .getClass() method, and all
objects inherit this.
Q4 Unanswered.
Q5 Answered.

//The following code is in file Go.java.


package Exam;
public class Go {
public static void main(String[] args) {
Test t1 = new TestSub();
t1.methodGo();
}
}
public class Test{
public void methodGo(){
System.out.println("In Super");
}
}
public class TestSub extends Test{
public void methodGo(){
System.out.println("In Sub");
}
}
What will this code print? Select 1 option.
A) Does not compile.
B) “In Super”
C) “In Sub”
D) Runtime Exception
The answer is A. Read all questions on the associate's exam very carefully.
Only one outer class in a package is allowed to have the "public" modifier,
and it must be the class with the same name as the java file it is contained in.
So in the above code, only class Go should have the public modifier.
Q5 Unanswered.
Q6 Answered.
package Exam;
public class Construct extends SuperConstruct{
Construct(int i) {
super(i);
}
Construct(String i) {
super(i);
}
Construct() {
super();
}
public static void main(String[] args) {
Construct c = new Construct();
}
}
class SuperConstruct{
SuperConstruct(int i){
System.out.println("Constructed with integer");
}
SuperConstruct(String str){
System.out.println("Constructed with string");
}
}
What will the following code print? Select 1 option.
A) “Constructed with integer”
B) “Constructed with string”
C) It will print nothing.
D) It will not compile.
E) It will throw a runtime exception.
The answer is D. If the super class has explicitly defined constructors, the
JVM will not create the implicit no-args constructor. The above code calls
the sub class no-args constructor which tries to call the super class no-args
constructor, which does not exist.
Q6 Unanswered.
Q7 Answered.
When constructing a service, to what file do we add ‘provides’ syntax?
A) service.java
B) module.java
C) module-info.java
D) main.java
The answer is C.
Q7 Unanswered.
Q8 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Integer i = new Integer(12);
Long l = new Long(12);
Double d = new Double(12);
System.out.println(i.equals(l));
System.out.println(l.equals(d));
}
}
What is the output from the program above? Select 1 option.
A) “true” followed by “true”
B) “false” followed by “true”
C) “false” followed by “false”
D) “true” followed by “false”
E) Does not compile.
The answer is C. Many java.lang classes have had .equals() overridden, but
the java.lang.Number classes have not. For Integer and Long, the .equals
method checks to see if the variables are pointing to the exact same object. It
is not checking the value.
Q8 Unanswered.
Q9 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
System.out.println("Beginning");
Test q = new Test();
q.throwException();
System.out.println("Past finally block");
}
public void throwException(){
try{
int[] arr = new int[5];
int i = arr[5];
}finally{
System.out.println("In finally block");
}
}
}
What is the last text printed by the code? Select one option.
A) Does not compile.
B) “Beginning”
C) “In finally block”
D) “Past finally block”
E) Nothing is printed because of the runtime exception.
The answer is C. A finally block always runs, even if an exception is thrown
in a try block and is not caught. In above code, the application fails after
running the finally block.
Q9 Unanswered.
Q10 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
q.go(1, "Will", " this", “ work?” );
}
public void go(int i, String...k){
for(String l:k){
System.out.print(l);
}
}
}
What does the code print? Select one option.
A) Compile time error
B) Runtime error
C) “Will this work?”
The answer is C. Method go(int I, String…k) has a varargs component. In
this case, the method can accept zero to unlimited string objects. It must be
passed one int primitive.
Q10 Unanswered.
Q11 Answered.
package Exam;
public class GC {
int g = 10;
short c = 20;
public static void main(String[] args) {
GC gc = new GC();//Line 1
gc.g = 12;
gc.c = 18; //Line 2
gc.go(); //Line 3
System.gc(); //Line 4
System.out.println(gc.g); //Line 5
}
public void go(){
System.out.println("go");
}
}
After which line is object gc eligible for garbage collection?
Select 1 option.
A) Line 1
B) Line 2
C) Line 3
D) Line 4
E) Line 5
The answer is E. System.gc() is only a suggestion to the JVM to run the
garbage collector. An object is actually eligible for garbage collection after it
has no more references to it remaining in the code.
Q11 Unanswered.
Q12 Answered.
package Exam;
import java.util.ArrayList;
import java.util.List;
public class Test {
List<String> dogs = new ArrayList<String>();
public static void main(String[] args) {
String dog1 = "German Shephard";
String dog2 = "Husky";
String dog3 = "Mutt";
Character dog4 = new Character('Shi Tzu');
Test q = new Test();
q.dogs.add(dog1);
q.dogs.add(dog2);
q.dogs.add(dog3);
q.dogs.add(dog4);
for(int i = 0; i<q.dogs.size(); i++){
System.out.print(q.dogs.get(i) + ",");
}
}
}
What will the output be from the above program? Select 1 answer.
A) “German Shephard,Husky, Mutt, Shi Tzu”
B) “German Shephard,Husky, Mutt, Shi Tzu,”
C) Compile time error.
D) None of the above
The answer is C. The first problem is that 'Shi Tzu' is an array of characters.
The Character API calls for only a single character, not for an array of
characters. Second, dog4 cannot be added to ArrayList dogs because dogs is
limited to a only containing String objects.
Q12 Unanswered.
Q13 Answered.
package Exam;
//Line 1
public class Test {
public static void main(String[] args) {
out.println("Hello World");
}
}
What can be inserted independently at Line 1 to make the following code
compile?
A) import java.lang.System.out;
B) import static java.lang.System.out;
C) import java.lang.System.*;
D) import static java.lang.System.*;
The answers are B, D. out is a static field in the class System in java.lang.
The word "static" is needed to import static fields. Choice D imports all static
fields of class System.
Q13 Unanswered.
Q14 Answered.
package Exam;
public class Test{
Object obj = new Object();
Integer i = new Integer(45);
public static void main(String[] args) {
Test q = new Test();
q.obj = "Hello";
q.obj = q.i;
System.out.println(q.obj);
}
}
What will the program print? Select 1 option.
A) “45”
B) Does not compile.
C) “Hello”
D) Runtime Error
The answer is A. Since "Hello" is a valid String object, "Hello" is assigned to
q.obj. But then q.obj is pointed to Integer q.i, which has the value 45.
System.out.println() calls at String.valueOf(x) to get the printed object's
string value.
Q14 Unanswered.
Q15 Answered.
package Exam;
public class FooledYou implements Fool {
static int r = 12;
public static void main(String[] args) {
Fool fy = new FooledYou;
fy.methodGo(Fool.r);
System.out.println(r);
}
public void methodGo(int r){
System.out.println(r);
}
}
interface Fool{
void methodGo(int r);
int r = 10;
}
What does the code print? Select 1 answer.
A) “12” followed by “12”
B) “10” followed by “10”
C) “10” followed by “12”
D) “12” followed by “10”
E) Does not compile.
The answer is C. 10 is printed first because Fool.r (which equals 10) is
explicitly called and passed to a method that does a println on the value
passed. 12 is then printed because fy.r is printed, and fy is instantiated as
FooledYou object, so its value is 12.
Q15 Unanswered.
Q16 Answered.
What will force a consuming module to import dependencies of a consumed
module?
A) requires transitive com.module
B) requires dependeny com.module
C) import transitive com.module
D) import dependency com.module
The answer is A. Each module dependency should have the keyword
transitive in front of it to avoid needing a require statement.
Q16 Unanswered.
Q17 Answered.
Which of the following is not a valid class modifier? Select 1 option.
A) public
B) private
C) protected
D) package private
E) package protected
The answer is E. However, D is not the actual text that would be used to
designate package private. Instead, no keyword is used. This is called the
default modifier.
Q17 Unanswered.
Q18 Answered.
package Exam;
public class Test {
String st;
int x = 10;
public static void main(String[] args) {
Test q = new Test();
System.out.println("Howdy");
if(q.x >5 | q.st.length() == 0){
System.out.println("Hello");
}
}
}
What does the code print? Select one option.
A) Does not compile.
B) “Howdy” followed by “Hello”
C) “Howdy”
D) Runtime Exception causes program to fail immediately.
E) Prints "Howdy" and then a Runtime Exception causes program failure.
The answer is E. The | operator evaluates both q.x > 5 and q.st.length() == 0
even though q.x > 5 is true. The || evaluates the right hand argument only if
the left hand argument returns false.
Q18 Unanswered.
Q19 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
int i = 12;
long L = 12;
float f = 12.0f;
double d = 12.2;
System.out.print(i == f);
System.out.print(i == d);
System.out.print(i.equals(f));
}
}
What will the following program print? Select 1 answer.
A) Does not compile.
B) “false false false”
C) “true true true”
D) “true false true”
E) “true false false”
The answer is A. .equals() is not a valid method for primitives. Line 1 would
print true and line 2 would print false if line three were removed.
Q19 Unanswered.
Q20 Answered.
package Exam;
public class Go {
public static void main(String[] args) {
TestSub ts = new TestSub();
ts.runThis();
}
}
class Test{
static{System.out.println("Super static");} //Line 1
{System.out.println("Super non-static");} //Line 2
public void runThis(){
System.out.println("Super runThis"); //Line 3
}
}
class TestSub extends Test{
static{System.out.println("Sub static");} //Line 4
public void runThis(){
System.out.println("Sub runThis"); //Line 5
}
{System.out.println("Sub non-static");} //Line 6
}
What is the following order of output? Select 1 answer.
A) 1, 2, 4, 5, 6
B) 4, 5, 6
C) 4, 6, 5
D) 1, 6, 4, 2, 5
E) 1, 4, 2, 6, 5
The answer is E. The rule for order of operations is:
1) Super class static blocks
2) Sub class static blocks
3) Super class non-static blocks
4) Constructor of the super class
5) Sub class non-static blocks
6) Constructor of the sub class
Q20 Unanswered.
Q21 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
q.subMethod();
}
public void subMethod(){
try{
subMethod2();
}catch(Exception e){
System.out.println("The Exception was caught here");
}
}
public void subMethod2(){
throw new ArrayIndexOutOfBoundsException();
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException”
C) “The Exception was caught here”
The answer is C. The exception is thrown up the chain of method calls until
either it is written to the console in the main method or it is handled in a try-
catch block.
Q21 Unanswered.
Q22 Answered.
package Exam;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List l = new ArrayList();
l.ensureCapacity(20);
System.out.print(l.size());
for(int i = 0; i<10; i++){
l.add(i);
}
l.trimToSize();
System.out.print(“ “ + l.size());
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “20 10”
C) “20 20”
D) “0 10”
The answer is A. The List interface does not have the method trimToSize().
That is a method that belongs to ArrayList. When l was declared as type List,
it was limited to the methods of the List interface. This is called “programing
to the API”.
Q22 Unanswered.
Q23 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
double d1 = 12.4;
double d2 = 12.40;
System.out.print(" " + Double.compare(d1, d2));
}
}

What is the output of the code? Select 1 answer.


A) Compile time error
B) “0”
C) “0”
The answer is C. The import statement is correct, and Double.compare is
also fine.
Q23 Unanswered.
Q24 Answered.
Which of the following are acceptable declarations? Select two answers.
A) String String;
B) String 123;
C) String volatile;
D) String $$$MONEY
E) String $$$money!!!
The answer is A, D. A variable cannot start with a number, volatile is a
reserved word, and ! is not an acceptable character in a variable name. Only
_ and $ are acceptable characters. Also, String is not a reserved word.
Q24 Unanswered.
Q25 Answered.
package Exam;
public class Test extends qqq {
public Test(int i, int j, Integer integer) {
//Line 1
}
public static void main(String[] args) {
Test q = new Test(1,2,new Integer("5"));
}
}
class qqq{
public int aa;
public int bb;
public int cc;
public qqq(int a, int b, Integer c){
int aa = a;
int bb = b;
int cc = c.intValue();
}
}
What is needed at Line 1? Select 1 answer.
A) A call to the default constructor of qqq.
B) A call to the explicit constructor qqq(int a, int b, Integer c)
C) Nothing is needed.
The answer is B. A subclass must have a call to a superclass constructor,
whether it is an implicit call or an explicit call. There is no implicit (default)
constructor of qqq since it has an explicitely defined constructor. Therefore
the subclass constructor must make a call such as the following: super(i, j,
integer);
Q25 Unanswered.
Q26 Answered.
package Exam;
public class Test extends SuperTest{
public static void main(String[] args) {
Test q = new Test();
try {
q.run();
} catch (Exception e) {
e.printStackTrace();
}
}
void run() throws Exception{
System.out.println("Overriding methods can throw more general
exceptions");
}
}
class SuperTest{
void run() throws FileNotFoundException{
System.out.println("Overridden methods must throw more specific
exceptions");
}
}
What is the output of the code? Select 1 answer.
A) “Overriding methods can throw more general exceptions”
B) “Overridden methods must throw more specific exceptions”
C) Does not compile
The answer is C. With checked exceptions, the overridden method must
throw more general exceptions or the same exception as the overriding
method.
Q26 Unanswered.
Q27 Answered.
package Exam;
public class Test extends SuperTest {
public static void main(String[] args) {
Test q = new Test();
q.returnThis();
}
public Number returnThis(){
Number doub = 2.2;
System.out.println(doub);
return integ;
}
}
class SuperTest{
public Double returnThis(){
Double num = 3d;
return num;
}
}
What does the following code print? Select one of the following.
A) “2.2”
B) “3”
C) Does not compile

The answer is C. An overriding method in a subclass must return the same


object or a subclass of the object that the overridden method returns.
Q27 Unanswered.
Q28 Answered.
package Exam;
import java.util.ArrayList;
public class Test {
ArrayList trainList = new ArrayList();
public static void main(String[] args) {
Test q = new Test(); //Line 1
q.addTrain("A1","Fort Worth","Amarillo",
q.trainList); //Line 2
q.addTrain("A2","Belen","Los Angeles",q.trainList);
q.addTrain("A3","Chicago","Kansas City",q.trainList);
System.out.println(((Train) q.trainList.get(1)).getTrainID());
}
public void addTrain(String id, String dep, String dest, ArrayList
tL){
Train trn = new Train(); //Line 3
trn.setTrainID(id);
trn.setDeparture(dep);
trn.setDestination(dest);
tL.add(trn); //Line 4
}
}
class Train{
private String trainID;
private String departure;
private String destination;
public String getTrainID() {
return trainID;
}
public void setTrainID(String trainID) {
this.trainID = trainID;
}
public String getDeparture() {
return departure;
}
public void setDeparture(String departure) {
this.departure = departure;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
}
Which line causes an Exception? Select one of the following.
A) Line 1
B) Line 2
C) Line 3
D) Line 4
E) The code is fine
The answer is E. This code is a good example of object oriented
programing. The Train class is used as a model. Multiple instances are
created and stored in an ArrayList to be accessed as needed.
Q28 Unanswered.
Q29 Answered.
package Exam;
import java.util.ArrayList;
import java.util.List;
public class Test{
List<Train> trainList = new ArrayList<Train>();
public static void main(String[] args) {
Test q = new Test();
q.addTrain("A1","Fort Worth","Amarillo", q.trainList);
q.addTrain("A2","Belen","Los Angeles",q.trainList);
q.addTrain("A3","Chicago","Kansas City",q.trainList);
System.out.println(q.trainList.get(1).getTrainID());
}
public void addTrain(String id, String dep, String dest, List<Train>
tL){
Train trn = new Train();
trn.setTrainID(id);
trn.setDeparture(dep);
trn.setDestination(dest);
tL.add(trn);
}
}
class Train{
private String trainID;
private String departure;
private String destination;
public String getTrainID() {
return trainID;
}
public void setTrainID(String trainID) {
this.trainID = trainID;
}
public String getDeparture() {
return departure;
}
public void setDeparture(String departure) {
this.departure = departure;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
}
What does the following code print? Select one of the following.
A) “A1”
B) “A2”
C) “A3”
D) Does not compile
The answer is B. This is similar to the previous question but uses generics in
the ArrayList (and also uses the List interface). List<Train> allows the List
to only contain train objects. This is called type safety. It also avoids
casting, which keeps the code cleaner.
Q29 Unanswered.
Q30 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Integer in = new Integer(5);
Double db = new Double(5.5);
System.out.println(in.compareTo(db));
}
}
What does the following code print? Select one of the following.
A) “1”
B) “0”
C) “-1”
D) Does not compile
The answer is D. Integer’s .compareTo() method requires an Integer object
inside the parenthesis. If db had been an Integer and been constructed with
an int value instead of the double value 5.5, the code would have compiled
and returned 0.
Q30 Unanswered.
Q31 Answered.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
Test q = new Test();
ArrayList tr = new ArrayList();
for(int i = 0; i<3; i++){
tr.add(new StringBuilder("str" + i));
}
q.change(tr);
System.out.println(tr.get(1));
}
public void change(ArrayList ls){
for(int i = 0; i < ls.size(); i++){
((StringBuilder) (ls.get(i)).append(" is here");
}
}
}
What does the following code print? Select one of the following.
A) “str0”
B) “str0 is here”
C) “str1”
D) “str1 is here”
E) Does not compile
The answer is E. ((StringBuilder) (ls.get(i)).append(" is here"); is not valid.
Since it is being accessed from the array, the compiler does not see
((StringBuilder) (ls.get(i)) as a variable, so it will not modify it. Other
methods are valid, such as .toString();
Q31 Unanswered.
Q32 Answered.
True or false? In java, object parameters are passed by value?
A) True
B) False
The answer is A. The tricky part is that java passes a copy of the memory
reference to the method, so both the original reference and the copy of the
reference are pointing to the same object in memory. Therefore, if the code
modifies the object parameter in the method, the original object will truly be
modified. To be succinct, java passes by value of reference for non-
primitives. The object itself is never passed.
Q32 Unanswered.
Q33 Answered.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
do{
int i = 0;
i++;
}while(i < 10);
System.out.println(i);
}
}
What does the following code print? Select one of the following.
A) “10”
B) “9”
C) Does not compile
The answer is C. i is declared inside the do{} statement. Therefore, it is not
visible to anything outside the do{} statement, including both the while()
statement and the System.out.println() statement.
Q33 Unanswered.
Q34 Answered.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
int i = 0;
while(i < 10)
do{
i++;
}
System.out.println(i);
}
}
What does the following code print? Select one of the following.
A) “10”
B) “9”
C) Does not compile
The answer is C. This code does not compile because do{} must come
before the while() statement, not after.
Q34 Unanswered.
Q35 Answered.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
int counter = 0;
for(int i = 5; i > 0; i--){
for(int x = 0; x < i; x++){
counter++;
}
}
System.out.println(counter);
}
}
What does the following code print? Select one of the following.
A) “5”
B) “10”
C) “15”
D) “20”
E) “25”
The answer is C. The outer loop executes 3 times before the condition x<i is
no longer satisfied.
Q35 Unanswered.
Q36 Answered.
package Exam;
import java.util.ArrayList;
public class Test{
public static void main(String[] args) {
ArrayList arr = new ArrayList();
int counter = 0;
outer: for(int i = 5; i > 0; i--){
inner: for(int x = 0; x < i; x++){
if(x == 3){
break inner;
}
counter++;
}
}
System.out.println(counter);
}
}
What does the following code print? Select one of the following.
A) “10”
B) “12”
C) “14”
D) Does not compile
The answer is B. If counter++ had been placed before the if() statement, the
answer would have been 14 because counter++ would have executed twice
more.
Q36 Unanswered.
Q37 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");;;;;;;;;;
}
}
True or false? This code compiles.
A) True
B) False
The answer is A. The compiler ignores the extra semicolons and the code
will print “Hello World”.
Q37 Unanswered.
Q38 Answered.
True or False? By default, java modules expose all of their API to other
modules?
A) True
B) False
The answer is B. If a module dependency needs to be exposed, it needs the
‘export’ keyword in front of the package name.
Q38 Unanswered.
Q39 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
Test q = new Test();
StringBuilder sb = new StringBuilder("This");
q.changeThis(sb);
System.out.println(sb);
}
public void changeThis(StringBuilder str){
System.out.print(str + " ");
str.delete(0, 4);
str.append("That");
}
}
What does the following code print? Select one of the following.
A) “This That”
B) “That That”
C) “This This”
D) Does not compile
The answer is A. Since java passes by value, str is pointing to the same space
(in other words, the same object) in memory as sb. Therefore when str is
modified then sb is modified.
Q39 Unanswered.
Q40 Answered.
Which of the following are true of getters and setters? Select 2 options.
A) Getters and setters increase code reusability.
B) Getters and setters are an integral part of Object Oriented Programing
C) Getters and setters are control the flow of the program
D) Getters and setters maintain encapsulation
The answers are B, D. Getters and setters are instance level methods in a
class that are used to set and access instance level variables. This allows the
variables themselves to have the private modifier. It is good OO (object
oriented) programing practice for methods of a class to control the
interactions of other objects with that class.
For example, why should the object Dog be able to directly access and
change the height attribute of the object Person? Modifiers on the getter and
setter methods for height would control what objects can view or change
height.
Q40 Unanswered.
Q41 Answered.
What is an interface with a single abstract method called?
A) It doesn’t have a name
B) Method Interface
C) Functional Interface
D) Abstract Interface
The answer is C. If there is exactly one abstract method, and zero, one, or
many default methods, the interface is a functional interface.
Q41 Unanswered.
Q42 Answered.
package Exam;
@FunctionalInterface
interface Cube {
int calculateVolume(int x);
int countSides(int y);
}
True or False: This code compiles
A) True
B) False
The answer is B. Because there is more than one abstract class, the compiler
throws an “Unexpected @FunctionalInterface annotation” message.
Q42 Unanswered.
Q43 Answered.
package Exam;
interface TestInterface {
public void add(int x);
default void print() {
System.out.println("Default Method Executed");
}
default void printAnother() {
System.out.println("Another Default Method Executed");
}
}
public class Test implements TestInterface {
public void add(int x) {
System.out.println(x+x);
}
public static void main(String args[]) {
Test writer = new Test();
writer.add(4);
writer.print();
}
}
True or False: This code compiles
A) True
B) False
The answer is A. The class does not call printAnother, but that is acceptable.
Implementing classes are not required to call all default methods.
Q43 Unanswered.
Q44 Answered.
True or False: Static methods on an interface can be overridden
A) True
B) False
The answer is B. If an interface with a static method is implemented by a
class, and the class then implements a method with an identical method
signature as the static method on the interface, the interface’s static method
will still exist on the interface and the class’s static method will exist on the
class.
Q44 Unanswered.
Q45 Answered.
package Exam;
interface LambdaInterface {
public void printer(String x);
default void printAnother() {
System.out.println("Another Print!“);
}
}
public class Test {
public static void main(String args[]) {
LambdaInterface inter = (String x) ->
System.out.println(x);
inter.printer(“Print Me!”);
}
}
What does the following code print? Select one of the following.
A) “Print Me!”
B) “Another Print!”
C) Does not compile
The answer is A. This is a straitforward example of a lambda function
implementing an abstract method on an interface.
Q45 Unanswered.
Q46 Answered.
package Exam;
interface LambdaInterface {
public void printer(String x);
public void anotherPrinter();
default void printAnother() {
System.out.println("Another Print!“);
}
}
public class Test {
public static void main(String args[]) {
LambdaInterface inter = (String x) ->
System.out.println(x);
LambdaInterface anotherInter = () ->
System.out.println("Print This!”);
inter.printer(“Print Me!”);
}
}

What does the following code print? Select one of the following.
A) “Print Me!”
B) “Print This!”
C) “Another Print!”
D) Does not compile
The answer is D. The compiler does not know which abstract function is
supposed to receive the lambda function implementation. This is also why an
interface with two abstract functions is not a functional interface.
Q46 Unanswered.
Q47 Answered.
package Exam;
import java.clock.LocalDate;
public class HelloWorld{
public static void main(String []args){
LocalDate local = LocalDate.new();
System.out.println(local);
}
}
What does the following code print? Select one of the following.
A) The current time
B) The current date
C) The current time and date
D) Does not compile
The answer is D. LocalDate is part of the java.time package, so the import
should have been java.time.LocalDate
Q47 Unanswered.
Q48 Answered.
Which of the following is not a valid java.time.LocalDate method?
A) atTIme
B) equals
C) getDayOfYear
D) getWeekDay
E) isLeapYear
The answer is D. There is no method getWeekDay, there is however a
method named getDayOfWeek. atTime combines a date with a time to create
a LocalDateTime object. Equals compares two dates for equality.
getDayOfYear returns a value from 1 to 366 (due to leap years). isLeapYear
returns a boolean value of whether a LocalDate object is a leap year.
Q48 Unanswered.
Q49 Answered.
What is the default value for the Boolean object?
A) true
B) false
C) undefined
D) null
The answer is D. The Boolean object, not to be confused with the boolean
primitive, has a default of null. The boolean primitive has a default of false.
Q49 Unanswered.
Q50 Answered.
What is the size in bytes of the int primitive?
A) 1 byte
B) 2 bytes
C) 4 bytes
D) 8 bytes
The answer is C. The int primitive can hold 4 bytes of data which makes it
capable of storing a value between -2,147,483,648 and 2,147,483, 647
Q50 Unanswered.
Q51 Answered.
Which syntax creates an optional, compile-time-only dependency?
A) requires devDependency com.module
B) requires static com.module
C) import optional com.module
The answer is B.
Q51 Unanswered.
Q52 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
Test q = new Test();
StringBuilder sb = new StringBuilder("This");
q.changeThis(sb);
System.out.println(sb);
}
public void changeThis(StringBuilder str){
System.out.print(str + " "); //Line 1
str = new StringBuilder("That"); //Line 2
}
}
What does the following code print? Select one of the following.
A) “This That”
B) “That That”
C) “This This”
D) Does not compile
The answer is C. Remember that java passes by value. Line 2 points str at a
new location/object in the JVM memory, so the original object that str
pointed at is not affected by the code in line 2.
Q52 Unanswered.
Q53 Answered.
The following code snippet is valid.
static int i1, i2[], i3;
Select 1 option.
A) True
B) False
The answer is A. The code snippet will compile. However, it is difficult to
work with and not recommended. The JVM will not compile if the next line
of code is:
i2[] = new int[10];
This is recommended:
static int i2[] = new int[10];
Q53 Unanswered.
Q54 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
float f = 5.0;
Test q = new Test ();
q.runInt(f);
}
public void runInt(float f){
Integer i = new Integer(new Float(f).intValue());
System.out.print(i );
}
}
What is the output of the code? Select one option.
A) “5”
B) “5.0”
C) Compile time exception
D) Runtime exception
The answer is C. float f = 5.0; is not valid. The JVM reads that line as trying
to assign a double to a float, and it will not demote double to float because
data could be lost.
Q54 Unanswered.
Q55 Answered.
What command line command will show all modules?
A) java --show-modules
B) java --list-modules
C) java --module-chart
D) java --modules
The answer is B.
Q55 Unanswered.
Test 2 Answers
Q1 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
String s = "hello";
StringBuilder sb = new StringBuilder("world");
System.out.println(q.addString(s));
System.out.println(q.addSB(sb));
System.out.println(s);
System.out.println(sb);
}
public String addString(String str){
str += " world";
return str;
}
public StringBuilder addSB(StringBuilder strbl){
strbl.insert(0, "hello ");
return strbl;
}
}
What will the code print?
A) "hello world" four times
B) "hello" followed by "hello world" followed by "hello" followed by "hello
world"
C) "hello" followed by "world" followed by "hello" followed by "world"
D) "hello world" twice followed by "hello" followed by "hello world"
The answer is D. String and StringBuilder are both objects, so they are
passed by reference. However, String is immutable, StringBuilder is not. So
s is never truly changed. Also, in the addString() method, str += creates a
new String object, so return str returns a String object with the value "hello
world".
Q1 Unanswered.
Q2 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
switch(2){
case 1: System.out.println("1");
case 2: System.out.println("2");
case 3: System.out.println("3");
default: System.out.println("None of the above");
}
}
}
What does the code print? Select one option.
A) Prints "2"
B) Prints "1", "2", "3", "None of the above"
C) Prints "2", "3"
D) Prints "2", "3", "None of the above"
E) Does not compile.
The answer is D. The case statement selects case 2, but then it falls through
all the cases after case 2.
Q2 Unanswered.
Q3 Answered.
package Exam;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new
FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append('\n');
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();
}
}catch (Exception e) {
System.out.println("Exception caught");
}catch (IOException e) {
System.out.println("IOException caught");
}
}
}
If file.txt is null, what will the code generate? Select 1 option.
A) The code will print "Exception caught"
B) The code will not compile even if file.txt is not null.
C) The code will print "IOException caught"
D) The code will throw an exception that is not caught, causing the program
to end.
The answer is B. The catch block for the IOException must come before the
catch block for Exceptions. Otherwise the code will because the compiler
sees that the code in the IOException catch block cannot be thrown.
Q3 Unanswered.
Q4 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
q.methodRun();
System.out.println("Complete");
}
public void methodRun();
public void methodHide(){
}
}
abstract class TestAbstract {
abstract void methodRun();
abstract void methodHide();
abstract void methodSeek();
}
What is the output from this program? Select one option.
A) “Complete”
B) Does not compile
C) Run time error
The answer is B. methodRun() in subclass Test is abstract since it has no
body. Therefore, it would have to be labeled abstract or else the app will not
compile. However, only classes labeled as abstract can have abstract
methods, so Test would need to be abstract and could not have a constructor
method.
Q4 Unanswered.
Q5 Answered.
package exam
public class Test {
public static void main(String[] args) {
int[][] myInt = {{2, 3, 1}, {4, 5, 6}, {7, 8, 9}};
System.out.println(myInt[1][1]);
System.out.println(myInt[3][1]);
}
}
What is the output from the code? Choose one answer.
A) “5” followed by “ArrayIndexOutOfBoundsException”
B) “2” followed by “7”
C) “2” followed by “1”
D) “5” followed by “7”
E) Does not compile
The answer is A. myInt[1][1] has a value of 5 because Java starts counting at
index zero. int[10] has 10 elements at positions 0-9. The code above also
compiles but has a runtime exception at myInt[3][1] because myInt only has
indices 0-2 for its first dimension.
Q5 Unanswered.
Q6 Answered.
Which of these are valid ArrayList object methods? Select 3 options.
A) .size()
B) .isEmpty()
C) .length()
D) .contains()
E) .removeElement()
The answers are A, B, D. .length() is a method of String object. Also, length
is a field in an array object.
.removeElement() is not a valid method. .remove() is a valid method of
ArrayList class and takes an object as a parameter. It removes the first
occurance of that object in the ArrayList.
Q6 Unanswered.
Q7 Answered.
package Exam;
import java.sql.Array;
import java.util.ArrayList;
public class Test {
Short a = new Short((short) 1);
Integer b = new Integer(1);
public static void main(String[] args) {
Test t = new Test();
if (t.b instanceof Short){
System.out.println("Test");
}else System.out.println("Failed");
}
}
What will the code print? Select 1 option.
A) “Test”
B) “Failed”
C) Will not compile.
D) Runtime Exception
E) Prints nothing.
The answer is C. This does not evaluate to false. Instead, the compiler sees
that b is of class Short, which can never be of class Integer.
Q7 Unanswered.
Q8 Answered.
package Exam;
public class Test {
int i = 0;
public static void main(String[] args) {
System.out.println("Hello World");
}
}
True or False: i is a class variable
A) True
B) False
The answer is B. i is an instance variable. To be a class variable, it needs the
modifier 'static'.
Q8 Unanswered.
Q9 Answered.
package Exam;
import java.util.ArrayList;
import java.util.List;
public class Test {
List<String> dogs = new ArrayList<String>();
public static void main(String[] args) {
String dog1 = "German Shephard";
String dog2 = "Husky";
String dog3 = "Mutt";
Character dog4 = new Character('s');
Test q = new Test();
q.dogs.add(dog1);
q.dogs.add(dog2);
q.dogs.add(dog3);
for(int i = 0; i<dogs.size(); i++){
System.out.print(q.dogs.get(i) + ",");
}
}
}

What will the output be from the above program? Select 1 answer.
A) “German Shephard,Husky, Mutt,”
B) “German Shephard,Husky, Mutt”
C) Compile time error.
D) None of the above
The answer is C. The reference to ArrayList dogs in the for loop needs to
read q.dogs because the ArrayList is not static. It is a member of the q
instance of class Q.
Q9 Unanswered.
Q10 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Integer i = new Integer(1); //Line 1
Number n = i; //Line 2
System.out.println(n.intValue()); //Line 3
i = n; //Line 4
System.out.println(i.intValue()); //Line 5
}
}
What can be changed to make this code compile? Select one option.
A) Change line 4 to i = (Integer) n;
B) Change line 2 to n = (Number) i;
C) Change the method called in Line 3 and 5 to doubleValue()
D) Change line 5 to System.out.println(i.parseInt("1"));
E) No changes need to be made.
The answer is A. In line 2, n is declared to be of type Number, which is a
superclass of Integer, so no explicit casting is needed. In line 4, i was
previously declared to be a subclass of Number, and the compiler sees n as an
object of class number, so i must be explicitly cast.
Q10 Unanswered.
Q11 Answered.
package Exam;
public class Test {
int i = 0;
public static void main(String[] args) {
//Line 1
for(i = 0; i<10; i++){
System.out.println("Hello World");
}
}
}
What changes can be made to the following code so that it will compile?
Select 2 options.
A) No changes are required to compile
B) Insert the following code at Line 1: Test q = new Test(); Then change i to
q.i
C) Declare i as type int within the for statement
D) No changes are required to run
E) Replace 'i = 0;' with 'i;'
The answers are B, C. i in the for() statement is referencing instance variable
i. Therefore it must be called using an instance, such as q.i Answer C makes
i in the for() statement a local variable, thus it does not need to be called by
its instance name.
Q11 Unanswered.
Q12 Answered.
package Exam;
public class B extends A {
public static void main(String[] args) {
A q = new B();
q.methodA1();
q.methodA2();
}
static void methodA1(){
System.out.println("Static B1,");
}
void methodA2(){
System.out.println(" B2");
}
}
class A{
static void methodA1(){
System.out.println("Static A1,");
}
void methodA2(){
System.out.println(" A2");
}
}
What does the code print? Select one option.
A) “Static A1, A2”
B) “Static A1, B2”
C) “Static B1, A2”
D) “Static B1, B2”
The answer is B. Since methodA1 is static, it is shadowed in class B instead
of overridden.
Q12 Unanswered.
Q13 Answered.
package Exam;
public class TestSub extends Test {
public static void main(String[] args) {
Test q = new TestSub();
q.out();
}
public void out(){
System.out.println("Sub out");
}
}
class Test {
protected void out(){
System.out.println("Super out");
}
}
What will the code print? Select 1 option.
A) Does not compile.
B) “Sub out”
C) “Super out”
D) Prints nothing.
The answer is B. The code compiles because a sub class can make an
inherited method's access modifier broader. The overridden method cannot
make the access modifier narrower. Also, the actual object q is type TestSub,
so the code runs TestSub.out().
Q13 Unanswered.
Q14 Answered.
True or False: The ! operator returns a boolean. Select one option.
A) True
B) False
The answer is A. An example of a situation in which ! can be used is an if
statement. if statements need a boolean returned in the evaluation, such as
if(x != y).
Q14 Unanswered.
Q15 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
double r = Math.random() * 10; //Line 1
Integer x = new Integer(r); //Line 2
System.out.println(x); //Line 3
}
}
What can be added to the following code to make it compile? Select 1
option.
A) It already compiles.
B) Append .intValue() to Integer(r).
C) Declare r as int in line 1.
D) Cast r as int in line 2.
The answer is D. Line 2 should read Integer x = new Integer((int)r);. The
Integer() constructor only takes int and String values as its parameter.
Q15 Unanswered.
Q16 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
String s = "hello world";
System.out.println(s.equals("hello world"));
System.out.println(s.concat(s.charAt(0)));
}
}
What will the code print? Select 1 option.
A) "true" followed by "hello worldh"
B) "false" followed by "hello worldh"
C) Compile time error
D) "true" and the a runtime error is thrown
The answer is C. s.charAt() is returns a char value. However, s.concat() only
accepts String values to be passed into it.
Q16 Unanswered.
Q17 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
int[][] myInt[] = {{{1,2},{3,4}},{{1,2},{3,4,5},{6}},{{7}}};
System.out.println(myInt[0][1][0]);
System.out.println(myInt[1][1][2]);
System.out.println(myInt[2][0][0]);
}
}
What is the output from the code? Choose one answer.
A) “5” followed by ArrayIndexOutOfBoundsException
B) “2” followed by “7” followed by “1”
C) “2” followed by “1” followed by “6”
D) Does not compile
E) “3” followed by “5” followed by “7”
The answer is E.
Q17 Unanswered.
Q18 Answered.
Which of the following correctly describe a method's signature? Select 1
option.
A) The method's access modifier, return type, name, and parameters.
B) The method's return type, name, and parameters.
C) The method's name and parameters.
D) The method's name.
The answer is C. A method with the same name and different parameters is
considered overloaded. There cannot be two methods with the same name
and parameters.
Q18 Unanswered.
Q19 Answered.
package Exam;
public class TestSub extends Test {
public static void main(String[] args) {
Test q = new TestSub();
q.out();
}
public void out(){
System.out.println("Sub out");
}
}
class Test {
private void out(){
System.out.println("Super out");
}
}
What will the code print? Select 1 option.
A) Does not compile.
B) “Sub out”
C) “Super out”
D) Prints nothing.
The answer is A. Since q is declared as type Test, the JVM looks to see
whether out() is a valid method in Test. It is a private method in class Test,
thus not visible from class TestSub. q.out() actually calls the method out() in
class TestSub if method out() is visible in Test.
Q19 Unanswered.
Q20 Answered.
package Exam
public class Test {
String str1 = "Hello World!";
String str2;
public static void main(String[] args) {
Test q = new Test();
if (q.str1.equals("Hello World!") | q.str2.equals("")){
System.out.println(q.str1);
}else System.out.println(q.str2.concat("Hello"));
}
}
What does the code print? Select one answer.
A) "Hello World!"
B) Compile time error
C) The code prints a blank line
D) "Hello"
E) Runtime Exception
The answer is E. The | operator evaluates both parts of the if statement even
if the first part returns true. The || operator would not have evaluated the
second part of the statement if the first returned true. This is called "short
circuiting". The runtime exception is thrown because str2 is null, and the
code tries to perform an operation against it.
Q20 Unanswered.
Q21 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
try{
q.subMethod();
}catch(Exception e){
System.out.print(" …Or the Exception was caught here");
}
}
public void subMethod(){
subMethod2();
System.out.print("The Exception was handled in subMethod2");
}
public void subMethod2(){
throw new ArrayIndexOutOfBoundsException();
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “The Exception was handled in subMethod2 …Or the Exception was
caught here”
C) "The Exception was handled in subMethod2"
D) " …Or the Exception was caught here"
The answer is D. subMethod() only executes its first line of code. An
Exception is thrown in the method that line of code calls (subMethod2()), so
the Exception is thrown back up the method chain until it is handled in the
main method. The main method then continues on with its next line of code.
Q21 Unanswered.
Q22 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
j = 0;
int j;
StringBuilder newString = new StringBuilder("");
while(j<4.5){
newString.append("Hello World".substring(j, j+1));
j++;
}
System.out.println(newString);
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “Hello”
C) "Heelllloo "
D) ""
The answer is A. A variable cannot be instantiated after it is assigned a
value. So j = 0; int j; is not valid.
Q22 Unanswered.
Q23 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
int j;
j = 0;
String newString = new String("");
while(j<=5){
newString.concat("Hello World".substring(j, j+1));
j++;
}
System.out.println(newString);
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “Hello ”
C) "Hello"
D) ""
The answer is D. String is immutable, so the concatenation is lost
immediately after the line
newString.concat("Hello World".substring(j, j+1));
Q23 Unanswered.
Q24 Answered.
package Exam;
public class Test extends Abs { //Line 1
public static void main(String[] args) {
}
public void doThis(){
}
public void doThat(){
}
}
abstract class Abs implements Ione, Itwo{ //Line 2
}
interface Ione{
public void doThis(); //Line 3
}
interface Itwo{
public void doThat(){ //Line 4
}; //Line 5
}
Where is the error in the code? Select 1 answer.
A) Line 1
B) Line 2
C) Line 3
D) Line 4
E) Line 5
The answer is D. All methods in an interface are abstract. Therefore they
cannot have a body. Line 5 is fine because the compiler ignores the
semicolon after a bracket.
Q24 Unanswered.
Q25 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
double d1;
if(d1<3){
d1=3;
}
for(int i = 0; i < d1; i+=2, d1+=1){
System.out.print(d1);
}
}
}
What is the output of the code? Select 1 answer.
A) Compile time error
B) “345”
C) "3.04.05.0"
D) "3456"
E) “3.04.05.06.0”
The answer is A. d1 is a local variable so it must be given a value. Only
instance level variables have default values. If d1 had been assigned a value
of 0, the answer would be C.
Q25 Unanswered.
Q26 Answered.
package Exam;
public class Test extends SuperTest{
public static void main(String[] args) {
Test q = new Test();
try {
q.run();
} catch (Exception e) {
e.printStackTrace();
}
}
void run() throws Exception{
System.out.println("This will run");
}
}
class SuperTest{
void run() throws ArrayIndexOutOfBoundsException {
System.out.println("Actually, this will run");
}
}
What is the output of the code? Select 1 answer.
A) “This will run”
B) “Actually, this will run”
C) Does not compile
The answer is C. Overriding methods must throw a subclass Exception of
what the overridden method throws even if the exception is a
RuntimeException. ArrayIndexOutOfBoundsException is a
RuntimeException.
Q26 Unanswered.
Q27 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
String str1 = new String("String 1");
String str2 = new String("String 2");
System.out.println(str2.compareTo(str1));
}
}
What is the output of the code? Select 1 answer.
A) “-1”
B) “0”
C) “1”
D) Does not compile
The answer is C. String implements the Comparable interface, which in the
String class compares two Strings lexicographically (comparison of the alphabetical order
of each character in each String). System.out.println(str2.compareTo(str1)); prints
“-1”.
Q27 Unanswered.
Q28 Answered.
package Exam;
import java.util.ArrayList;
public class Test {
ArrayList rec = new ArrayList();
public static void main(String[] args) {
Test q = new Test();
Rectangle r1 = q.new Rectangle(2,3);
Rectangle r2 = q.new Rectangle(3,4);
Rectangle r3 = q.new Rectangle(4,5);
q.rec.add(r1);
q.rec.add(r2);
q.rec.add(r3);
for(int i = 0; i < q.rec.size(); i++){
if((i%2)==0){
System.out.println(((Rectangle) (q.rec.get(i))).getArea());
}
}
}
class Rectangle{
private int side1;
private int side2;
public void setSide1(int x){
side1 = x;
}
public int getSide1(){
return side1;
}
public void setSide2(int y){
side2 = y;
}
public int getSide2(){
return side2;
}
public int getArea(){
return side1*side2;
}
public Rectangle(int s1,int s2){
setSide1(s1);
setSide2(s2);
}
}
}
What is the output of the code? Select 1 answer.
A) “6” followed by “20”
B) “6” followed by “12” followed by “20”
C) “12”
D) Does not compile
E) Runtime exception
The answer is A. The inner class works fine, the casting is proper (although
generics are preferable), and the encapsulation of the inner class is excellent.
Q28 Unanswered.
Q29 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
int rate = 10;
double amount = rate/100;
System.out.println(amount);
}
}
What is the output of the code? Select 1 answer.
A) “1”
B) “0”
C) “.1”
D) Does not compile
The answer is B. rate/100 is evaluated as an int since rate is an int.
Therefore, anything right of the decimal is truncated. To return the double
value, the code would be double amount = (double) rate/100;
Q29 Unanswered.
Q30 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
char c = 49;
System.out.println(c);
}
}
True or false: The code prints “49”.
A) True
B) False
The answer is B. The code prints 1. char c = 49 assigns the character with
ASCII value 49 to c. 1 is the character with ASCII value 49.
Q30 Unanswered.
Q31 Answered.
What are the benefits of object-oriented programing (OOP)? Select three
answers.
A) Code is socketable or modular, so a class’s source code can be
maintained independently of the rest of an application’s code.
B) An object’s code is kept private, meaning other objects only interact with
an object’s methods, so the internal code is kept hidden.
C) Objects are easily reusable. They can be plugged into multiple parts of
the code.
D) Object-oriented code executes faster and computer resource usage is
minimized.
The answers are A, B, C. A is describing the fact that an object can be
modified without needing to recode anywhere else in an app. For example, if
the product class of a Point of Sale system needs to be expanded to meet new
business needs, there is likely nothing wrong with the rest of the code. OOP
allows the product class to be modified while leaving the rest of the code as-
is.
B is useful for proprietary code. For example, if a company develops and
sells code that plugs into an existing customer application, the proprietary
code needs to be kept invisible to the customer. If the customer only interacts
with methods, the variables and logic of the classes in the code are kept
proprietary.
C describes the ability of a class to be plugged into an app in multiple
locations, or even into multiple apps. Older programming languages were
linear in their execution. OOP lets an object be called repeatedly without
having to rewrite the same code repeatedly.
Q31 Unanswered.
Q32 Answered.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
try{
ArrayList arr = new ArrayList();
arr.add(0, "element 0");
System.out.println(arr.get(1));
}catch(IndexOutOfBoundsException e){
System.out.println("Cannot access. There are only " + arr.size() +
" elements.");
}
}
}
What does the code print? Select one option.
A) “element 0”
B) “Cannot access. There are only 1 elements.”
C) Does not compile
The answer is C. arr is not accessible in the catch block because it is a local
variable that only exists in the try block. In other words, the scope of arr is
limited to the try block.
Q32 Unanswered.
Q33 Answered.
package Exam;
import java.util.ArrayList;
//Line 1
public class Test{
public static void main(String[] args) {
Test2 t = new Test2();
System.out.println(t.x);
}
}
class Test2{
public int x = 100;
}
True or false? Line 1 should be the code import Exam.Test2;
A) True
B) False
The answer is B. Since Test and Test2 are in the same package, no import
statement is needed.
Q33 Unanswered.
Q34 Answered.
True or false? An instance level variable can be accessed anywhere in a class
as long as an instance has been created.
A) True
B) False
The answer is B. An instance level variable cannot be accessed in a static
method or a static block.
Q34 Unanswered.
Q35 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
int i;
if(5%3>2){
i=0;
}else if(5%3>1){
i=1;
}
}
}
Why will this code not compile? Select one option.
A) It compiles fine
B) 5%3>2 and 5%3>1 do not return Booleans and thus cannot be used in an
if statement
C) String[] args is never used
D) i is a local variable and its value is never explicitly defined
The answer is A. Local variables must always be initialized. However, the
compiler can look at these if statements and see that i is certainly going to be
initialized.
Q35 Unanswered.
Q36 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
int i;
int x = 2;
if(5%x>2){
i=0;
}else if(5%x>1){
i=1;
}
}
}
True or false? This code compiles.
A) True
B) False
The answer is A. Local variables such as i must always be initialized before
they are used. i is never used, so the code compiles. It is always best to
initialize a local variable. Also, if the compiler can see that an if statement
will definitely execute and initialize a variable, the code will compile. In this
case, even though an if statement would execute, the compiler will not allow
the code because a variable is contained in the if statement. x may be 2
currently, but the compiler cannot see if it will be changed somewhere else in
the code before the if statement executes.
Q36 Unanswered.
Q37 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
q.throwException();
}
public void throwException(){
try{
System.out.println("Get this".substring(1,9));
}catch(Exception e){
}
}
}
What does the code print? Select one option.
A) “Get this”
B) Prints nothing
C) Does not compile
The answer is B. An exception is thrown from System.out.println("Get
this".substring(1,9));, but the exception is caught. There is no code in the
catch block, so nothing is printed.
Q37 Unanswered.
Q38 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
System.out.println("Beginning");
Test q = new Test();
q.throwException();
System.out.println("Past finally block");
}
public void throwException(){
try{
catchException();
}catch(Exception e){
System.out.println("thrown");
}
}
public void catchException() throws Exception{
throw new Exception();
}
}
What is the last line the code prints? Select one option.
A) “Beginning”
B) “thrown”
C) “Past finally block”
The answer is C. This is called chained exceptions. The main method calls
throwException, which calls catchException. The exception thrown in
catchException is handled in throwException.
Q38 Unanswered.
Q39 Answered.
Which of the following instantiates the object Dog? Select 2 options.
A) public Dog{}
B) public void Dog{}
C) public void Dog(){}
D) public Dog{};
The answer is A, D. The compiler ignores the semicolon in D. C is a method
signature for the method Dog(), while B is syntax that would not compile.
Q39 Unanswered.
Q40 Answered.
True or false? A class level variable can be accessed anywhere in a class.
A) True
B) False
The answer is A. A class level variable, also known as a static variable, can
be accessed in any method, including static methods and constructors.
Q40 Unanswered.
Q42 Answered.
package Exam;
interface TestInterface {
default void print() {
System.out.println("Not Important");
}
static void printImportant() {
System.out.println(“Important”);
}
}
public class Test implements TestInterface {
static void printImportant() {
System.out.println(“Really Important”);
}
public static void main(String args[]) {
printImportant();
}
}
Which of the following is the output of the code? Select one option.
A) “Important” followed by “Really Important”
B) “Important”
C) “Really Important”
D) Compile time error
The answer is C. The static method in the interface can be accessed as
follows: TestInterface.printImportant();
Q42 Unanswered.
Q43 Answered.
What is the default value of any Object?
A) null
B) undefined
C) 0
D) There is no default value common to all objects
The answer is A.
Q43 Unanswered.
Q44 Answered.
True or False: static variables cannot be modified in non-static methods?
A) True
B) False
The answer is B. Any method on a class can modify the static properties of
that class.
Q44 Unanswered.
Q45 Answered.
Public Exam;
public class Tester{
private int myInt = 5;
public static void main(String []args){
Tester test = new Tester();
int myInt = 10;
System.out.println(test.incrementInt(test.myInt));
}
public int incrementInt (int theirInt) {
return theirInt++;
}
}
What does the code print?
A) 5
B) 6
C) 10
D) 11
The answer is A. test.myInt is the instance specific myInt, not the method
specific myInt. Also, ++ doesn’t increment the value in incrementInt until
after the value is returned.
Q45 Unanswered.
Q46 Answered.
Which of the following are true about arrays and ArrayLists? Select all
applicable answers.
A) Arrays have a fixed length and ArrayLists do not
B) ArrayList can hold primitives, arrays can only hold objects
C) ArrayList implements the Collection interface
D) Array implements the List interface
The correct options are A, C. ArrayList can only hold objects (primitives
stored in an ArrayList are actually converted to objects). ArrayList
implements List interface, array does not.
Q46 Unanswered.
Q47 Answered.
import java.util.function.Predicate;
public class PredicateTest{
public static void main(String []args){
Predicate<String> containsS = testString -> (testString.contains("s"));
System.out.println(containsS("cows"));
}
}
What does the code print?
A) true
B) false
C) “s”
D) Does not compile
The answer is D. The code should read
System.out.println(containsS.test(“cows”)); Notice the .test that’s missing.
Q47 Unanswered.
Q48 Answered.
import java.util.function.Predicate;
public class PredicateTest{
public static void main(String []args){
Predicate<String> containsS = testString -> (testString.contains("s"));
Predicate<String> containsT = testString -> (testString.contains("t"));
System.out.println(containsS.and(containsT).test("tricksy"));
}
}
What does the code print?
A) “s”
B) “t”
C) true
D) false
E) Does not compile
The answer is C. This is called predicate chaining. Both methods are run
and, due to the “and” keyword, both must be true in order for the expression
to evaluate as true.
Q48 Unanswered.
Q49 Answered.
package Exam;
public class Tester{
public static void main(String []args){
Tester test = new Tester();
int myInt = 3;
System.out.println(++myInt++);
}
}
What does the code print?
A) 3
B) 4
C) 5
D) Does not comile
The answer is D. It is not valid to have the ++ operator both before and after
a value.
Q49 Unanswered.
Q50 Answered.
package Exam;
public class Tester{
private static int myInt = 1;
public static void main(String []args){
Tester test = new Tester();
int myInt = 3;
System.out.println(test.myInt);
}
}
What will the code print?
A) 1
B) 3
C) Does not compile
The answer is A. Static variables are available for access from instance
objects. Keep in mind, however, that a static variable is class level and thus
one instance changing it’s value makes it a different value for all instances.
Q50 Unanswered.
Test 3 Answers
Q1 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
int i = 0;
char a = 'a';
short s = 1;
i = a;
System.out.println(i);
}
}
What is printed out by the code? Select 1 option.
A) “0”
B) “1”
C) A value greater than 1
D) A value less than zero
The answer is C. The ascii value of the character 'a' is 97. This is the value
assigned to i and then printed.
Q1 Unanswered.
Q2 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
String s = " hello world ";
System.out.println(s.trim());
}
}
What will the code print? Select 1 option.
A) " hello world"
B) "hello world "
C) "hello world"
D) "helloworld"
The answer is C. s.trim() will trim all leading and following spaces.
Q2 Unanswered.
Q3 Answered.
Which of the following is not a valid declaration and instantiation of an
array? Select
two options.
A) int[][][] myInt = new int[1][2][3];
B) int[][][] myInt = new int[1][][];
C) int[][][] myInt = new int[][][3];
D) int[][] myInt[] = new int[1][][];
E) int[1][2][3] myInt = new int[][][];
The answers are C and E. The outer dimensions of an array must be
specified before inner dimensions. Also, dimensions can only be sized on the
right hand side of the operation.
Q3 Unanswered.
Q4 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
String this = "Team USA";
String that = "Team UK";
this.concat(that);
System.out.println(this);
modify(this,that);
System.out.println(this);
s=modify(this, that);
System.out.println(this);
}
public static String modify(String one, String two){
String mod = "";
mod = two.concat(" and") + one.substring(4);
return mod;
}
}
Which of the following is the output of the code? Select one option.
A) “Team USA” followed by “Team USA” followed by “Team UK and
Team USA”
B) “Team USA and Team UK” followed by “Team USA and Team UK”
followed by “Team UK and Team USA”
C) “Team USA” followed by “Team USA and Team UK ” followed by
“Team UK and Team USA”
D) “Team USA and Team UK” followed by “Team USA ” followed by
“Team UK and Team USA”
E) Compile time error
The answer is E. “this” is a reserved word in java and cannot be used as a
variable name.
Q4 Unanswered.
Q5 Answered.
double x = 10;
Which of the following code snippets will assign x's value to str? Select 1
option.
A) String str = x.toString();
B) Stinrg str = "";
str = x.toString();
C) String str = Double.toString(x);
D) Stinrg str = "";
str = Double.toString(x);
E) None of the above.
The answer is C. A is incorrect syntax. D does not assign 10 to str because
strings are
immutable. Once they are given a value, that value is never changed.
Q5 Unanswered.
Q6 Answered.
package Exam;
public class qq1 {
public static void main(String[] args){
int count = 1;
System.out.println(count);
System.out.println(++count);
System.out.println(count++);
System.out.println(count);
}
}
What does the code print? Select one option.
A) “1123”
B) “1122”
C) “1133”
D) “1233”
E) “1223”
The answer is E.
Q6 Unanswered.
Q7 Answered.
package Exam
public class Test {
public static void main(String[] args) {
int[][][] myInt = new int[1][2][3];
int[][] yourInt = {{1,2},{3,4}};
myInt[0] = yourInt[][];
System.out.println(myInt[0][1][0]);
}
}
True or false: the code is valid.
A) True
B) False
The answer is B. The constructor myInt[0] = yourInt[][]; is a misplaced
constructor.
Q7 Unanswered.
Q8 Answered.
package Exam;
public class Test {
static int i = 0;
static short s = 0;
static byte b = 0;
static double d = 0;
static char c = '0';
public static void main(String[] args) {
System.out.println(i == s);
System.out.println(b == d);
System.out.println(c == i);
}
}
How many times will the code print 'true'? Select 1 option.
A) 0
B) 1
C) 2
D) 3
The answer is C. System.out.println(c == i); prints false because the ascii
value of
'0' is not zero. The ascii value of ' ' is zero, so if c = ' ', then the answer
would be D.
Q8 Unanswered.
Q9 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
String s = "hello world";
System.out.println(s.append("!!!"));
System.out.println(s.reverse());
}
}
What will the code print? Select 1 option.
A) "hello world!!!" followed by "!!!dlrow olleh"
B) "hello world!!!" followed by "dlrow olleh"
C) "hello world" followed by "dlrow olleh"
D) Compile time error
The answer is D. .append() and .reverse() are methods of the StringBuilder
class.
Q9 Unanswered.
Q10 Answered.
package Exam;
public class Test {
String s = "hello";
StringBuilder sb = "world";
public static void main(String[] args) {
System.out.println(s.concat(sb.toString()));
}
}
What does the code print? Select one option.
A) “helloworld”
B) “worldhello”
C) Does not compile
The answer is C. The proper instantiation for StringBuilder is StringBuilder
sb = new StringBuilder(“world”);
Q10 Unanswered.
Q11 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
int r = 0;
for(int xx = 0; xx < 10; xx+=r++){
System.out.println(++r);
}
}
}
What does the code print?
A) “1,3,5,7,9,”
B) “2,4,6,8,”
C) “1,2,3,4,5,6,7,8,9”
D) “1,3,5,7,”
E) “2,4,6,8,10”
The answer is D. Since xx is adding r each loop, the values of x are 1, 4, 9,
16. Thus, r is 1, 3, 5, 7
Q11 Unanswered.
Q12 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
for(x = 0; x < 5; ++x){
System.out.print(++x + ",");
int y = 0;
for(y;y<x;y+=x){
System.out.print(y +",");
}
}
}
}
What will the code print? Select one option.
A) Compile time error
B) “1,1,3,3,5,5,”
C) “1,0,3,0,5,0,”
D) “1,2,3,4,5,”
The answer is A. x is not declared anywhere in the code. Furthermore, the
code in the second for loop is not correct. y must be initialized to a value in
the for() statement.
Q12 Unanswered.
Q13 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
String a = "stringy";
switch(a){
case "stringy": System.out.println("Strings work");
break;
case "string": System.out.println("Strings don't work");
}
}
}
What does the code print? Select one option.
A) Compile time error
B) “Strings work”
C) “Strings don’t work”
D) Runtime error
The answer is B. Strings are valid input for case statements since Java SE7.
Q13 Unanswered.
Q14 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
boolean a = true;
switch(a){
case true: System.out.println("Booleans work");
break;
case false: System.out.println("Booleans don’t work");
}
}
}
What does the code print? Select one option.
A) Compile time error
B) “Booleans work”
C) “Booleans don’t work”
D) Runtime error
The answer is A. Booleans are not valid to be used in switch case statements
as of Java SE 8.
Q14 Unanswered.
Q15 Answered.
package Exam;
public class Test extends Build{
public static void main(String[] args) {
Test q = new Test ();
StringBuilder sb = new StringBuilder("");
q.display(sb);
q.display();
}
private void display(StringBuilder ss){
ss.append("America".substring(4, 5));
ss.append("America".substring(1, 2));
ss.append("America".substring(6, 7));
System.out.println(ss);
}
}
class Build{
void display(){
StringBuilder ___ = new StringBuilder("");
___.append("Mexico".substring(0, 2));
System.out.println(___);
}
}
What is the output of the code? Select one answer?
A) Compile time error
B) “Me” followed by “Me”
C) “ima” followed by “Me”
D) “ima” followed by “ima”
The answer is C. display() is inherited by class Test. Also, display is
overloaded in Test so that there are now two similarly named but distinct
methods: display() and display(StringBuilder). Also, ___ is a valid name for
a class. It is just poor convention.
Q15 Unanswered.
Q16 Answered.
package Exam;
public class Test extends TestSuper {
public static void main(String[] args) {
Test qq = new Test();
qq.out();
}
}
class TestSuper {
protected void out(){
System.out.println("1 out");
}
}
What does the code print? Select one answer.
A) Does not compile
B) “2 out”
C) “1 out”
D) Runtime error
The answer is C. The protected modifier on TestSuper.out() means that
classes in the same package have visibility to the method.
Q16 Unanswered.
Q17 Answered.
List ls = new ArrayList(); //line 1
ArrayList ls = new ArrayList(); //line2
Which of the following is generally a better coding standard?
A) line 1
B) line 2
The answer is A. Declaring an object to be a different type than what it is
instantiated as can be useful for constraining the types of methods accessible
by an object, among other reasons. This is called “programing to the API”.
Q17 Unanswered.
Q18 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
q.go(5, "this", "that");
q.go(6, "this");
}
public void go(int i, String...k){
for(String l:k){
System.out.print(l);
}
}
public void go(int i, String k){
System.out.println(k + " done");
}
}
What does the code print? Select one option.
A) Compile time error
B) “thisthat” followed by “this”
C) “5thisthat” followed by “6this”
D) “thisthat” followed by “this done”
The answer is D. go(int I, String…k) contains a varargs parameter. Any
number of strings can be passed to this method. Also, q.go(6, “this”) is
passed to go(int I, String k) because this is the more specific method. The
JVM will always prefer to invoke the most specific method possible.
Q18 Unanswered.
Q19 Answered.
package Exam;
import java.util.ArrayList;
public class ArrListTest {
public static ArrayList arrLst = new ArrayList();
public static void main(String[] args) {
populateArr();
}
public static void populateArr(){
for(int i = 1; i <= 10; i++){
arrLst.add(i);
}
for (int x = 0; x<=9; ++x){
System.out.println(arrLst.get(x));
}
}
}
What will the code print? Select 1 answer.
A) The numbers 1 through 10
B) The numbers 1 through 9
C) The numbers 2 through 10
D) It will not compile.
The answer is A. In a standard for() loop, the incrementing of the looping var
happens after all code in the brackets {} has been executed. Therefore, ++i
and i++ have the same results in a for loop.
Q19 Unanswered.
Q20 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
char c = 'a'; //line 1
int x = c; //line 2
double y = x; //line 3
short z = c; //line 4
char cc = z; //line 5
}
}
Which of the lines of code do not compile? There may be multiple answers.
A) line 1
B) line 2
C) line 3
D) line 4
E) line 5
The answer is D, E. Line 2 assigns the ascii code of ‘a’ (which is 97) to int x,
which is a valid integer. The JVM can implicitly cast the int as a double
without loss of data in line 3, and does so. Line 4 and line 5 fail because the
JVM cannot widen short to char or widen char to short. Doing so risks losing
data.
Q20 Unanswered.
Q21 Answered.
package Exam;
public class Test extends Abs { //Line 1
public static void main(String[] args) {
}
}
abstract class Abs implements Ione, Itwo{ //Line 2
}
interface Ione{ //Line 3
public void doThis();
}
interface Itwo{ //Line 4
public void doThat();
}
Which of the lines of code do not compile? Select one answer.
A) line 1
B) line 2
C) line 3
D) line 4
The answer is A. Since class Test is not abstract, it must implement the
methods that exist in the interfaces that it implements. The code would be
public void doThis(){} public void doThat(){}
Q21 Unanswered.
Q22 Answered.
package Exam;
public class Test {
static boolean b;
public static void main(String[] args) {
int integ = 0;
if(b){
integ=2;
}
for(int i = 0; i < integ; i+=2, integ+=1.5){
System.out.print(integ);
}
}
}
What is the output of the code? Select one answer
A) Compile time error
B) “23”
C) “”
D) “24”
E) An infinite loop is entered with output “”
The answer is C. The default value of Boolean variables is false, so integ is at
value 0 when the for() statement is executed. Therefore, the for statement
does no loops since i<integ is never true. Also, integ+=1.5 is the same as
integ+=1 since the decimal is truncated for integers.
Q22 Unanswered.
Q23 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test(); //Line 1
StringBuilder z = q.runThis(); //Line 2
System.out.println(z);
}
StringBuilder public runThis(){ //Line 3
StringBuilder x = new StringBuilder("abc");
StringBuilder y = new StringBuilder("xyz");
StringBuilder z = new StringBuilder("");
for(int i = 0; i < x.length(); i++){
z.append(x.charAt(i)); //Line 4
z.append(y.charAt(i));
}
return z;
}
}
Which of the lines of code do not compile? Select one answer.
A) line 1
B) line 2
C) line 3
D) line 4
E) There is no problem and the code output is “axbycz”
The answer is C. The return type must immediately precede the name of the
method.
Q23 Unanswered.
Q24 Answered.
package Exam;
public class Test{
static boolean a;
static boolean b = true;
static boolean c = false;
public static void main(String[] args) {
if(a == c != b){
System.out.print("One is true");
}else{
System.out.print("One is false");
}
}
}
What is the output of the code? Select one answer.
A) “One is true”
B) “One is false”
The answer is B. The code first evaluates (a == c) which evaluates to true
since a is the default boolean value of false and c was instantiated with the
value false. The code then compares (true != b) which is the same as (true !=
true), which evaluates to false.
Q24 Unanswered.
Q25 Answered.
package Exam;
public class True{
static boolean a = true;
static boolean b = true;
static boolean c = false;
public static void main(String[] args) {
if(a = c != b){
System.out.print("Two is true");
}else{
System.out.print("Two is false");
}
}
}
What is the output of the code? Select one answer.
A) “Two is true”
B) “Two is false”
The answer is A. The code evaluates (c!=b) first since the right hand side of
an assignment operation always evaluates first. This evaluates to (false !=
true) which is true. Then the left hand side is evaluated: (a = true). So a is
set to true and the if statement is simply if(true).
Q25 Unanswered.
Q26 Answered.
Which of the following is not a type of inner class?
A) static member class
B) static local class
C) non-static member class
D) non static local class
E) non-static anonymous class
The answer is B. An anonymous class has no instance name, just the new
keyword. For example, button.addActionListener(new ActionListener{…})
is an example of an anonymous inner class.
Q26 Unanswered.
Q27 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
char c = '10';
System.out.println(c);
}
}
True or false: The code prints “10”.
A) True
B) False
The answer is B. This code does not compile. char c = ‘9’ is valid. char c =
‘10’ is not valid. c can only hold one character at a time.
Q27 Unanswered.
Q28 Answered.
Which of the following is not a reason to use nested classes?
A) It is a way to coherently group classes used in only one part of the code
B) It cuts down on the total number of classes used in the code
C) It increases encapsulation
D) It often leads to more readable and maintainable code
The answer is B.
Q28 Unanswered.
Q29 Answered.
package Exam;
public class Test {
int side1;
int side2;
private static String name;
public static void main(String[] args) {
Test q = new Test ("Rectangle",4,5);
whatName(q);
}
static void whatName(Test q){
System.out.println(name + ". Area is " + q.side1*q.side2);
}
public Test (String shape, int one, int two){
side1 = one;
side2 = two;
name = shape;
}
}
What does the code print? Select one option.
A) “null. Area is 20”
B) “Rectangle. Area is ”
C) “Rectangle. Area is 20”
D) Does not compile
The answer is C. Instance level variables cannot be accessed from a static
method, but whatName had an instance of Test passed to it, so it was able to
access those instance level variables.
Q29 Unanswered.
Q30 Answered.
What is the proper way to instantiate a static nested object?
A) OuterClass.StaticNestedClass nestedInstance = new
OuterClass.StaticNestedClass();
B) StaticNestedClass nestedInstance = new StaticNestedClass();
The answer is A. The nested class must be referenced by the name of the
outer class.
Q30 Unanswered.
Q31 Answered.
Which of the following is not an example of inheritance? Select one option.
A) An object has a field that its superclass has
B) An object has a method that its superclass has
C) An object has a method that its interface has
D) An object has a static field that its superclass has
The answer is C. Interfaces are not inherited. An interface is similar to an
abstract class, but multiple interfaces can be implemented by one object.
Q31 Unanswered.
Q32 Answered.
True or false? In Java, an object can have only one subclass but unlimited
superclasses.
A) True
B) False
The answer is B. An object can have only one superclass but unlimited
subclasses. For example, Vehicle class may have SUV, Coup and Truck as
subclasses. It may have Transport as its superclass, but it cannot have any
other superclass.
Q32 Unanswered.
Q33 Answered.
True or false? An object can implement multiple interfaces.
A) True
B) False
The answer is A. Allowing implementation of multiple interfaces is one way
that Java gets around the limitations of allowing objects only to have one
superclass. For example, the Bat class may implement both Flyer and
Mammal interfaces. This means it would have to contain all methods from
both of those interfaces, but that makes sense because a bat has all
characteristics of both a mammal and a flyer.
Q33 Unanswered.
Q34 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
byte x = 100;
byte y = 100;
byte z = x+y;
System.out.println(z);
}
}
What does the code print? Select one option.
A) “200”
B) “-56”
C) “-55”
D) “-54”
E) Does not compile
The answer is E. Two bytes cannot be added together without implicit
casting to an integer. If z were declared an int this would work.
Q34 Unanswered.
Q35 Answered.
True or False? A String is a primitive data type.
A) True
B) False
The answer is B. A String is an object, not a primitive. Primitives are
reserved words that define what values a field can hold.
Q35 Unanswered.
Q36 Answered.
package Exam;
public class Test {
String x;
public static void main(String[] args) {
Test q = new Test();
if(q.x != null & !q.x.equals("")){
System.out.println("x is " + q.x);
}else{
System.out.println("No value for x");
}
}
}
What does the code print? Select one option.
A) “x is x”
B) “No value for x”
C) Does not compile
D) Runtime Exception
The answer is D. If the && conditional operator had been used instead of &,
this code would have run and the answer would have been B. However, &
will evaluate both statements (in this case q.x != null and !q.x.equals("")).
This fails because q.x is null, so the .equals() method cannot be called on x.
Q36 Unanswered.
Q37 Answered.
String str = " this".concat("that ").substring(3).trim().valueOf(2);
What is the value of str after the above code executes?
A) 2
B) s
C) t
D) Does not compile
The answer is A. " this".concat("that ").substring(3).trim() has a value of
“isthat “, but the valueOf(int) ignores all of this and returns a String
representation of the parameter int. It is not finding a value at an index.
This is a static method simply used for returning a String object.
Q37 Unanswered.
Q38 Answered.
package Exam;
public class Car implements Vehicle{
int engine = 1; //Line 1
int speed = 5;
String color = "Silver";
public static void main(String[] args) { //Line 2
}
public int getEngineSize(){ //Line 3
return engine;
}
public int getSpeed(){ //Line 4
return speed;
}
public String getPaint(){
return color;
}
}
interface Vehicle{ //Line 5
double getEngineSize();
int getSpeed();
String getPaint();
}
Which line of code causes the compile time issue? Select one option.
A) Line 1
B) Line 2
C) Line 3
D) Line 4
E) Line 5
F) The code compiles as-is
The answer is C. The return type of the implemented methods must be the
same as that of the abstract method in the interface.
Q38 Unanswered.
Q39 Answered.
Which of the following are valid modifiers of class? Select four options.
A) private
B) final
C) abstract
D) public
E) default
The answers are B, C, D, E. Private is not permitted because a class should
not exist that cannot be accessed by any other class. This would be poor
OOP design, since classes are meant to carry out particular roles as part of a
larger system. Package-private (default) is as close as a programmer can
come to declaring a class private; it would be private to members outside of
its package.
Q39 Unanswered.
Q40 Answered.
Which of the following is not a reserved word in java? Select one option.
A) abstract
B) compile
C) do
D) enum
E) goto
The answer is B; compile is not reserved. Also, goto is reserved but it has no
function. There is speculation that goto is reserved because it has a meaning
in other programming languages but often has unintended consequences
when used.s
Q40 Unanswered.
Q41 Answered.
Package Exam;
public class Tester{
private static int classX = 10;
public static void main(String []args){
Tester test = new Tester();
classX = 11;
System.out.println(test.accessClassX());
}
public int accessClassX () {
return ++classX;
}
}
What does the code print? Select one option.
A) “10”
B) “11”
C) “12”
D) Does not compile
The answer is C. Both static and non-static methods can modify static
variables. Also, ++ operates before the value is returned.
Q41 Unanswered.
Q42 Answered.
Package Exam;
public class Tester{
private int myInt = 5;
public static void main(String []args){
Tester test = new Tester();
test.myInt = 6;
System.out.println(test.incrementIntAgain(test.incrementInt(test.myInt)));
}
public int incrementInt (int theirInt) {
return theirInt++;
}
public int incrementIntAgain (int theirInt) {
return ++theirInt;
}
}
What is the output of this code?
A) 5
B) 6
C) 7
D) 8
The answer is C. Passing the returned value of one method as the parameter
of another method is fine.

Q42 Unanswered.
Q43 Answered.
True or False: an instance level object can access a static variable?
A) True
B) False
The answer is A. If testObj is an instance of Test class, and Test class has a
static int x, testObj.x is a valid accessor of the static int x value.
Q43 Unanswered.
Q44 Answered.
True or False? An ArrayList can store primitives.
A) True
B) False
The answer is B. While code such as the following is valid, the value is
actually converted to an Integer before being store inside the ArrayList:
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(5);
Q44 Unanswered.
Q45 Answered.
Which of the following is not a Collection interface method?
A) boolean isEmpty()
B) int size()
C) List getData()
D) boolean contains(Object element)
The answer is C. The other methods are available on Classes that implement
Collection, such as ArrayList.
Q45 Unanswered.
Q46 Answered.
import java.util.function.Predicate;
public class PredicateTest{
Predicate<String> containsS = testString -> (testString.contains("money"));
public static void main(String []args){
System.out.println(containsS.test("bank"));
}
}
What does the code print?
A) true
B) false
C) Does not compile
The answer is C. containsS is a non-static method here and it must be
accessed by an instance of PredicateTest.
Q46 Unanswered.
Q47 Answered.
import java.util.function.Predicate;
public class PredicateTest{
public static void main(String []args){
Predicate<Integer> greaterThan = intValue -> (intValue > 0);
Predicate<Integer> lessThan = intValue -> (intValue < 100);
System.out.println(greaterThan.or(lessThan).test(-5));
}
}
What will the code print?
A) true
B) false
C) Does not compile
The answer is A. -5 satisfies the condition of the lessThan predicate. The
‘or’ operator is used, therefore only one of the two conditions need to be
satisfied to return a value of true.
Q47 Unanswered.
Q48 Answered.
package Exam;
public class AnonymousTest{
public static void main(String []args){
System.out.println(new String("5"));
}
}
True or False? The following code does not compile.
A) True
B) False
The answer is B. This code does compile and prints “5”. A String is created
without a variable reference to it.
Q48 Unanswered.
Q49 Answered.
package Exam;
public class ConversionTest{
public static void main(String []args){
int x = 10;
double y = 10.5;
System.out.println(x + y);
}
}
What does the code print?
A) 20
B) 20.5
C) Does not compile
The answer is B. java creates a temporary double value in place of int x for
the purpose of being able to add two different data types.
Q49 Unanswered.
Q50 Answered.
True or False? A do-while loop always executes at least once.
A) True
B) False
The answer is A. The ‘do’ portion of the do-while is entered into before any
truthy evaluation is performed.
Q50 Unanswered.
Test 4 Answers
Q1 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.methodM());
}
public String methodM(){
if(4<5){
return "First return";
}else{
return "Second return";
}
return "Last return";
}
}
Which of the following is the output of the code? Select one option.
A) “First return”
B) “Second return”
C) “Last return”
D) Does not compile
The answer is D. The code would compile if the line ‘return “Last return”’
was not in the code. However, the JVM knows this code is unreachable
because something will be returned in the if-else statement. Also, the JVM
does not look inside the condition of the if statement to determine whether
the code in the if statement is reachable.
Q1 Unanswered
Q2 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
String financial = spends() ? "poor":"rich";
System.out.println(financial);
}
public static boolean spends(){
return false;
}
}
What is the output of the code? Select one option.
A) Does not compile
B) “rich”
C) “poor”
The answer is B. This form of the if statement is called the ternary
expression and is only used for conditional assignment. It is often seen as a
more concise way of writing code, but is not necessarily best practice.
Q2 Unanswered.
Q3 Answered.
package Exam;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.InputException;
public class Test {
public static void main(String[] args) {
BufferedReader reader = null;
try {
File file = new File("passTheTest.txt");
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (InputException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (InputException e) {
e.printStackTrace();
}
}
}
}
What issue is there with this code? Select one answer.
A) BufferedReader is not a real class
B) passTheTest.txt cannot be read by the File class
C) InputException does not need to be imported because it is in package
java.lang
D) InputException does not exist
The answer is D. The correct exception is IOException. InputException is
not a real class. C would not be the answer not only because InputException
is not real, but also because the compiler does not mind import statements for
the java.lang package.
Q3 Unanswered.
Q4 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
int r = 0;
do{
System.out.println("r equals " + r+2);
}while((r+=2) < 6);
}
}
What is the last line of output of the code? Select one option.
A) “r equals 62”
B) “r equals 8”
C) “r equals 42”
D) “r equals 4”
E) “r equals 22”
F) “r equals 2”
The answer is C. Since r is being converted to a string by the method
System.out.println(), then r+2 is read as a concatenation of two strings.
Q4 Unanswered.
Q5 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
int r = 0;
if(r+2 < 2){
System.out.println("Print this");
}
if(r+=2 < 3){
System.out.println("Print that");
}
}
}
What is the output of the code? Select one option.
A) Compile time error.
B) “Print this”
C) “Print that”
D) Run time error
The answer is A. The second if statement uses the += operand. In the code
above, it reads r+=2 < 3 as trying to pass in both an int and a Boolean since <
returns a Boolean value.
Q5 Unanswered.
Q6 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
double[] d = {1,2};
ArrayList l = aa(d, 3);
for(int i = 0; i < l.size(); i++){
System.out.print(l.get(i) + " ");
}
}
public static ArrayList aa(double[] a, double s) {
ArrayList<Double> arr = new ArrayList<Double>();
double nd = 0;
for (int i = 0; i < a.size(); i++) {
nd = s * a.get(i);
arr.add(nd);
}
return arr;
}
}
What is the output of the code? Select one option.
A) “3.0 6.0”
B) “3 6”
C) “2 4”
D) Does not compile
The answer is D. .size and .get() are methods of ArrayList, not array.
Q6 Unanswered.
Q7 Answered.
The native modifier is only valid for methods, not for variables or
constructors.
True or False?
A) True
B) False
The answer is true. Native is not tested extensively in the Associate
certification test, but you do need to know that the native modifier is only
valid for methods. The native modifier marks a method that will be
implemented in another language.
Q7 Unanswered.
Q8 Answered.
Which of the following are valid variable modifiers? Select one option.
A) public, native, final
B) transient, volatile, native
C) synchronized, final, static
D) final, transient, volatile, static
The answer is D. Synchronized and native are only valid modifiers of
methods.
Q8 Unanswered.
Q9 Answered.
Which of the following are all valid modifiers of methods? Select one
option.
A) final, abstract, synchronized, native
B) abstract, volatile, static
C) transient, static, public, synchronized
D) abstract, transient, public
The answer is A. Volatile and transient are variable modifiers.
Q9 Unanswered.
Q10 Answered.
package Exam;
public static void main(String[] args) {
loop:
for(int i = 0; i < 10; i++){
System.out.println(i);
if(i == 5){
break loop;
}else if((i%2) > 0){
continue;
}else{
i++;
}
}
}
}
What is the last line of output? Select one option.
A) “5”
B) “4”
C) “10”
D) “8”
E) Compile time error
The answer is D. The code is valid. for loops can be labeled and the syntax
above can be used for breaking out of one. However, i is never equal to five.
Q10 Unanswered.
Q11 Answered.
package Exam;
public static void main(String[] args) {
loopOuter:
for(int i = 0; i < 10; i++){
System.out.println(i);
while(i%5 > 0){
i++;
if(i > 3){
break;
}
continue loopOuter;
}
}
}
}
What is the output of the last line of code? Select one option.
A) “10”
B) “8”
C) “9”
D) “7”
The answer is B. The break statement breaks the inner loop, which is the
while loop. Also, labeling can be used with the continue command.
Q11 Unanswered.
Q12 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q1 = new Test();
Test q2 = new Test();
if(q1 instanceof q2){
System.out.println("Will this print?");
}else{
System.out.println("No");
}
}
}
What is the output of the code? Select one option.
A) “Will this print”
B) Compile time error
C) “No”
D) Run time error
The answer is B. The righthand side of the instanceof operator must be a
class, not an object. For example, (q1 instanceof Test) yields true.
Q12 Unanswered.
Q13 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
int i = 5;
float f = 5;
if(i == f){
double d = 5;
if(f == d){
System.out.println("Java promotes float to double");
}else{
System.out.println("Java will not promote float to double");
}
}else{
System.out.println("Java will not promote int to float");
}
}
}
What is final line of output of the code? Select one option.
A) “Java promotes float to double”
B) “Java will not promote float to double”
C) “Java will not promote int to float”
D) There is no output
The answer is A. Java promotes int to float and float to double when the ==
operand is used. So I == f compares 5.0 to 5.0.
Q13 Unanswered.
Q14 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
double d = 2.1;
float f = 4.131f;
if(d == 2){
System.out.println(d);
}
else if((int) f == 4){
System.out.println(f);
}
else{
System.out.println("Neither is equal");
}
}
}
What is the output of the code? Select one option.
A) “2.1”
B) “4.131”
C) “2”
D) “4”
E) “Neither is equal”
The answer is B. d==2 is a valid comparison that returns false. Casting f to
an int returns the value 4, but the value of f is not permanently changed.
Q14 Unanswered.
Q15 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
float f = 5.2f;
double r = 7.2;
Test q = new Test();
q.runInt(r);
q.runInt(f);
}
public void runInt(double r){
Integer i = new Integer(new Double(r).toString());
System.out.println(i + " ");
i = new Integer(new Double(r).intValue()*(int)3.2);
System.out.print(i + " ");
}
public void runInt(float f){
Integer i = new Integer(new Float(f).intValue() * (int) 1.1f);
System.out.print(i + " ");
}
}
What is the output of the code? Select one option.
A) “7 21 5”
B) “7.2 23.04 5.72”
C) Compile time exception
D) Runtime exception
The answer is D. Integer(new Double(r).toString()) is the same as
Integer(“7.2”). 7.2 is not an integer, and the Integer class will not demote 7.2
it to 7.
Q15 Unanswered.
Q16 Answered.
Given the following code:
package Exam;
public class Test {
public static void main(String[] args) {
int r = 5;
TC tc = new TC();
tc.tryThis(r);
System.out.println(r);
}
}
class TC {
public void tryThis(int i){
int x;
i += 1;
System.out.println(i+x);
}
}
What will be the output? Select 1 option.
A) “6” followed by “6”
B) “6” followed by “5”
C) “null” followed by “5”
D) “5” followed by “6”
E) Does not compile
The answer is E. The local variable x in tryThis(int) was never initialized.
Local vars must be initialized before being used. Also, adding 1 to i in
tryThis(int) would not affect the value of r in class q4, thus
System.out.println(r) would print 5.
Q16 Unanswered.
Q17 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Orange o = new Orange("Big Orange", 2);
System.out.println(o.getName() + “ “ + o.getDiameter());
}
}
class Orange{
private int diameter = 1;
private String name;
public void setName(String n){
this.name = n;
}
public String getName(){
return this.name;
}
public void setDiameter(int d){
this.diameter = d;
}
public int getDiameter(){
return this.diameter;
}
public Orange(String s, int d){
setName(s);
setDiameter(d);
}
public Orange(int d){
setName("Orange");
setDiameter(d);
}
}
What is the output of the code? Select one option.
A) “Orange 2”
B) “Big Orange 2”
C) “Orange 1”
D) “Big Orange 1”
The answer is B. The constructor called is Orange(String s, int d), thus both
the name and the diameter are given the values passed to the constructor.
Q17 Unanswered.
Q18 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
outer:
for(int i = 3; i< 7; i++){
for(int ii = i/2; ii < i; ii += 2){
if(ii > i*2){
continue outer;
}
else{
System.out.print(ii + " ");
break;
}
}
}
}
}
What is the output of the code? Select one option.
A) “1 2 2”
B) “1 2”
C) “0 1 2 3”
D) “1 2 2 3”
The answer is D. The outer loop is labeled “outer”, so any time that ii > i*2
then there is nothing printed. If ii <= i*2, then ii is printed and the inner loop
is broken.
Q18 Unanswered.
Q19 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
int i = 0;
int counter = 0;
while (i < 3){
counter++;
if("Hello world" == "Hello " + "world"){
i += 3%2;
}else{
counter+=i;
i++;
}
}
System.out.println(counter);
}
}
What is the output of the code? Select one option.
A) “3”
B) “5”
C) It enters an infinite loop
D) “6”
The answer is A. On strings, the == comparison checks to see that the two
exact same string objects are being compared. This is different than the
.equals() operator which for the string class compares whether the values are
equal.
Q19 Unanswered.
Q20 Answered.
How can a programmer make a field in a class invisible to all other classes?
There may be more than one answer.
A) Give the class the private modifier
B) Give the field the private modifier
C) No modifier is needed. By default, all fields and methods are private
until given a different modifier
D) Give the class the protected modifier
E) Give the field the protected modifier
The answer is B. Only public, abstract and final are valid modifiers for a
class. Also, by default all fields and methods are given the package protected
modifier, which means other classes in the same package can access the field
or method.
Q20 Unanswered.
Q21 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
StringBuilder z = q.runThis();
System.out.println(z);
}
public runThis(){
StringBuilder x = new StringBuilder("abcdef");
StringBuilder z = new StringBuilder("");
for(int i = 0; i < x.length(); i++){
z.append(x.charAt(i));
z.append(x.charAt(i+3));
}
return z;
}
}
What is the output of the code? Select one option.
A) Does not compile
B) “abcdef”
C) “adbecf”
The answer is A. public runThis(){} is the syntax for a constructor. A
method must have a return type designated, even if it is void.
Q21 Unanswered.
Q22 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
Test q = new Test ();
q.runThis();
}
public void runThis(){
StringBuilder x = new StringBuilder("abcdef");
StringBuilder z = new StringBuilder("");
boolean a;
for(int i = 0; i < x.length(); i++){
z.append(x.charAt(i));
if(a = true == true == false){
z.append(x.charAt(i+3));
}
}
System.out.println(z);
return;
}
}
What is the output of the code? Select one option.
A) Does not compile
B) “abcdef”
C) “adbecf”
D) prints nothing
The answer is B. A method can have a return statement even if the return
type is void. The return statement cannot actually return anything, however.
Q22 Unanswered.
Q23 Answered.
package Exam;
public class Test extends Build{
public static void main(String[] args) {
Test q = new Test();
q.display();
}
private void display(){
StringBuilder $1 = new StringBuilder("");
$1.append("Hello World".substring(2, 3));
$1.append("Hello World".substring(7, 9));
$1.append("Hello World".substring(10, 11));
System.out.println($1);
}
}

class Build{
private void display(){
StringBuilder $1 = new StringBuilder("");
$1.append("Hello World".substring(0, 1));
$1.append("Hello World".substring(7, 1));
$1.append("Hello World".substring(9, 1));
$1.append("Hello World".substring(10, 1));
System.out.println($1);
}
}
What is the output of the code? Select one option.
A) Compile time error
B) “Hold”
C) Runtime error
D) “lord”
The answer is D. display() is private in the Build class and so it is not
inherited in Test. Also, the substring() syntax is not correct in
Build.display().
Q23 Unanswered.
Q24 Answered.
package Exam;
public class Test extends Build{
public static void main(String[] args) {
Test q = new Test();
q.display();
}
private void display(){
StringBuilder $1 = new StringBuilder("");
$1.append("ChaChaCha".substring(0, 1));
$1.append("ChaChaCha ".substring(3, 4));
$1.append("ChaChaCha ".substring(6, 7));
System.out.println($1);
}
}
class Build{
void display(){
StringBuilder $1 = new StringBuilder("");
$1.append("ChaChaCha ".substring(2,3));
$1.append("ChaChaCha ".substring(7, 8));
$1.append("ChaChaCha ".substring(10, 11));
System.out.println($1);
}
}
What is the output of the code? Select one option.
A) Compile time error
B) “CCC”
C) Runtime error
D) “aaa”
The answer is A. Notice the access modifier is the default modifier for
Build.display() but it is private for Test.display(). This is not valid; a
subclass modifier cannot be more restrictive than a superclass modifier for an
overridden method.
Q24 Unanswered.
Q25 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
String num = "55";
int r = new Integer(num).intValue(); //line 1
double x = Double.valueOf(r); //line 2
r = x; //line 3
}
}
Which line causes a compile time error? Select one option.
A) line 1
B) line 2
C) line 3
D) none
The answer is C. Line 1 is conforms to the Integer class API. Line 2 uses
autoboxing to cast r as a double because Double.valueOf(double) requires a
double. Line 3 fails because the JVM will not automatically cast a double as
an int. Doing so could cause information loss.
Q25 Unanswered.
Q26 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
switch(args[0].charAt(0)){
case TestConstants.option1: System.out.println("Option 1
Selected");
case TestConstants.option2: System.out.println("Option 2
Selected");
case TestConstants.option3: System.out.println("Option 3
Selected");
}
}
}
class TestConstants{
public static final String option1 = “A”;
public static final String option2 = “B”;
public static final String option3 = “C”;
}
What does the following code print when run from the command line with
the command java Test C? Select one of the following.
A) “Option 1 Selected”
B) “Option 2 Selected”
C) “Option 3 Selected”
D) Does not compile
The answer is D. In java se 7, Strings are not valid to switch on. That is why
the switch statement switches on a char value. However, the compiler sees
that the case statements are Strings and gives a compile time error.
Q26 Unanswered.
Q27 Answered.
package Exam;
public class Test extends SuperTest{
public static void main(String[] args) {
SuperTest q = new Test();
System.out.println(q.s);
}
public void run(){
s="Sub";
}
}
class SuperTest{
String s;
SuperTest(){
run();
}
public void run(){
s = "Super";
}
}
What does the following code print when run from the command line with
the command java Test C? Select one of the following.
A) “Super”
B) “Sub”
C) Does not compile
The answer is B. Because q is instantiated as class Test, then Test’s methods
are called.
Q27 Unanswered.
Q28 Answered.
What is the code for instantiating an anonymous inner class? Select one of
the following.
A) String s = new String(“example”);
B) String s = “example”;
C) new String(“example”);
The answer is C.
Q28 Unanswered.
Q29 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
int[] numbers = {1,2,3,4,5};
for (int numies : numbers)
{
System.out.print(numies + “, “);
}
}
}
What is the output of the code? Select one option
A) “1, 2, 3, 4, 5, “
B) “5, 4, 3, 2, 1, “
C) Does not compile
The answer is A. This is the enhanced for() statement.
Q29 Unanswered.
Q30 Answered.
package Exam;
public class Test {
static int i = 0;
static short s = 0;
static byte b = 0;
static double d = 0;
static char c = '0';
public static void main(String[] args) {
s = d; //Line 1
i = c; //Line 2
c = (char) i; //Line 3
i = (int) d; //Line 4
}
}
Which lines compile properly? Select 3.
A) Line 1
B) Line 2
C) Line 3
D) Line 4
The answers are B, C, D. d must be cast to a short before it can be assigned
to s.
Q30 Unanswered.
Q31 Answered.
True or false? An interface can extend an object.
A) True
B) False
The answer is B. An interface can extend multiple other interfaces, but an
interface is not a concrete object itself and thus cannot extend/inherit from an
object. An interface most closely resembles an abstract class.
Q31 Unanswered.
Q32 Answered.
True or false? An object inherits all the fields and methods of its superclass.
A) True
B) False
The answer is B. An object does not inherit fields and methods that have the
“private” modifier.
Q32 Unanswered.
Q33 Answered.
Which of the following are valid modifiers of interface methods? Select two.
A) public
B) protected
C) private
D) abstract
The answers are A, D. All interface methods must be public and therefore
must be public in the classes that implement the interface. All methods in an
interface are abstract by definition because they do not have a body.
Q33 Unanswered.
Q34 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
Race m = new Marathon();
System.out.println(m.getName());
Race t = new Triathalon();
System.out.println(t.getName());
}
}
class Marathon implements Race{
String name = "marathon";
double distance = 10;
public double getDistance(){
return distance;
}
public String getName(){
return name;
}
}
class Triathlon implements Race{
String name = “triathlon”;
public double getDistance(){
return distance;
}
public String getName(){
return Race.name;
}
}
interface Race{
double distance = 26.2;
String name = "race";
String getName();
double getDistance();
}
What is the output of the code? Select 1 option.
A) “marathon” followed by “race”
B) “race” followed by “race”
C) “marathon” followed by “triathlon”
D) Does not compile
The answer is A. In class Marathon and Triathlon, name is hidden. To
access the interface name, Triathlon uses the code form Interface.field. If
Interface.field is not specified, java returns the field of the class the method
resides in. Also, both Marathon and Triathlon were declared type Race. This
affects the methods available to m and t. This does not affect the field
returned.
Q34 Unanswered.
Q35 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
try{
q.run();
}catch(ExceptionA x){
System.out.println("Caught it!");
}
}
public void run() throws ExceptionB{
throw new ExceptionB();
}
}
class ExceptionA extends Exception{}
class ExceptionB extends ExceptionA{}
True or false? The code prints “Caught it!”.
A) True
B) False
The answer is A. The catch block will catch the exception even though the
exception thrown is type ExceptionB. This is because the catch block will
catch ExceptionA and all of its subclasses.
Q35 Unanswered.
Q36 Answered.
package Exam;
public class Armadillo extends Critter {
public static void main(String[] args) {
Critter q = new Armadillo();
System.out.print(q.areTheyBig());
System.out.print(areTheyHairy());
}
public static String areTheyHairy(){
return "No";
}
public String areTheyBig(){
return "No ";
}
}
class Critter{
public static String areTheyHairy(){
return "Yes";
}
public String areTheyBig(){
return "Maybe ";
}
}
What does the code print? Select one answer.
A) “Maybe Yes”
B) “Maybe No”
C) “No Yes”
D) “No No”
The answer is D. Even though q is declared to be of type Critter, the methods
called are from Armadillo. The restriction is that no method can be called
from Armadillo that does not also exist in Critter.
Q36 Unanswered.
Q37 Answered.
What is the proper package and class name of checked exceptions? Select
one answer.
A) java.util.CheckedException
B) java.lang.RuntimeException
C) java.lang.Exception
D) java.util.Exception
E) java.lang.CheckedException
The answer is C. java.lang.RuntimeException is the only other real class in
the list, and it is the name of unchecked exceptions. A checked exception
must be explicitly handled in the code, an unchecked exception does not.
Also, unchecked exceptions can generally be avoided. For example,
IndexOutOfBoundsException can be avoided by checking the length of an
Array.
Q37 Unanswered.
Q38 Answered.
package Exam;
import java.util.ArrayList;
public class Test{
public static void main(String[] args) {
System.out.println(arrayOops());
}
public static String arrayOops(){
ArrayList arr = new ArrayList();
try{
System.out.println(arr.get(0));
}catch(RuntimeException e){
return "catch block";
}finally{
return "finally block";
}
}
}
What does the code print? Select one answer.
A) “catch block”
B) “finally block”
C) Compile time error
The answer is B. The finally block always executes, even if the catch block
has a return statement. So in this example, the exception was caught and the
catch block was entered, but instead of returning the code in the catch block
the finally block was entered.
Q38 Unanswered.
Q39 Answered.
Which modifiers allow access to a method or field by all other classes in the
same package? Select three answers.
A) public
B) protected
C) no modifier (package-protected)
D) private
The answers are A, B, C. Private allows no other class to access a method or
field, not even a subclass of the class the method or field is part of.
Q39 Unanswered.
Q40 Answered.
Which of the following are generally true regarding access modifiers?
A) Fields should have the most narrow access modifier possible
B) Methods should have the widest access modifier possible
C) Avoid using public fields unless the field is a constant
The answers are A, C. All members (both fields and methods) should have
the most narrow access modifier possible. This keeps methods from being
used in unintended ways and keeps fields from having their values changed
unintentionally.
Q40 Unanswered.
Q41 Answered.
True or False: It is necessary to use the @FunctionalInterface annotation on
functional interfaces?
A) True
B) False
The answer is B. The annotation is not necessary. The compiler will throw
an error if a lambda expression is used on a non-functional interface
regardless of whether or not the annotation has been used.
Q41 Unanswered.
Q42 Answered.
package Exam;
interface TestInterface {
public void add(int x);
default void print() {
System.out.println("Default Method Executed");
}
default void printAnother() {
System.out.println("Another Default Method Executed");
}
}
public class Test implements TestInterface {
public void add(int x) {
System.out.println(x+x);
}
public static void main(String args[]) {
Test writer = new Test();
writer.print();
}
}
Which of the following is the output of the code? Select one option.
A) “Default Method Executed”
B) “Another Default Method Executed”
C) Compile time error
The answer is A. An implementing class is not required to call all default
methods in an interface.
Q42 Unanswered.
Q43 Answered.
What class does ArrayList extend?
A) ArrayList does not extend any classes
B) AbstractList
C) Array
D) ListCollection
The answer is B. AbstractList is an abstract class that implements Collection
interface. Array is not a class.
Q43 Unanswered.
Q44 Answered.
package Exam;
public class CaseTest{
private String justInCase(String str){
String returner = "";
switch(str) {
case "a":
returner.concat("a");
case "b":
returner.concat("b");
default:
returner.concat("done");
}
return returner;
}
public static void main(String []args){
CaseTest ct = new CaseTest();
System.out.println(ct.justInCase("a"));
}
}
What does the code return?
A) “a”
B) “abdone”
C) “done”
D) an empty string
The answer is D. concat returns a new String, it does not mutate the original
string. So in this question, returner is not mutated by concat.
Q44 Unanswered.
Q45 Answered.
True or False? A try statement must always be followed by at least one catch
statement
A) True
B) False
The answer is B. A try statement may be followed by a finally statement
instead of a catch statement. A finally statement always executes.
Q45 Unanswered.
Q46 Answered.
True or False? Local classes are classes defined in a block of code. Local
classes are typically defined inside a method.
A) True
B) False
The answer is A. This is loosely quoted from the java docs.
Q46 Unanswered.
Q47 Answered.
Which of the following cannot be anonymous? Select all that apply.
A) Classes
B) Methods
C) Interfaces
D) Variables
The answer is C, D. Interfaces must be named to be used. Variables by
definition are not anonymous.
Q47 Unanswered.
Q48 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Integer[][] arr = {{0, 3, 12},{2, (Integer) null}};
System.out.println(arr[1][1]);
}
}
True or False? The (Integer) casting is required for the code to compile?
A) True
B) False
The answer is B. The compiler will handle the casting.
Q48 Unanswered.
Q49 Answered.
True or False? The “super” keyword cannot be used in any method except a
constructor.
A) True
B) False
The answer is B. super() can be used inside a method to make a call to the
parent class method of the same name.
Q49 Unanswered.
Q50 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
loop:
for(int i = 10; i > 0; i--){
System.out.println(i);
if(i == 3){
break loop;
}else if((i%3) > 0){
continue;
}else{
i-=2;
}
}
}
}
What is the last line printed by the code?
A) 3
B) 1
C) 6
D) 10
The answer is A. There are no syntax errors in this code.
Q50 Unanswered.
Test 5 Answers
Q1 Answered.
package Exam;
public class Test extends SuperTest {
public static void main(String[] args) {
System.out.println("This is all there is");
}
}
class SuperTest {
int x = 0;
String y = "super";
char z = 'z';
static {System.out.println("super print"); }
SuperTest(int x){
this.x = x;
}
SuperTest(String y){
this.y = y;
}
SuperTest(char z){
this.z = z;
}
}
Which of the following is the output of the code? Select one option.
A) “super print” followed by “This is all there is”
B) “This is all there is” followed by “super print”
C) “This is all there is”
D) Compile time error
The answer is D. When the application is run, it creates an instance of class
Test using the default constructor. This in turn tries to call the default
constructor of the super class, SuperTest. However, since SuperTest has
defined constructors, it does not have a default constructor. The compiler
sees this. **Note** Some IDEs such as Eclipse will still run the code despite
the compile time error. The output is “super print” followed by “This is all
there is”. The compiler will still issue a warning. However, for the exam the
answer would be D.
Q1 Unanswered.
Q2 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
int x = 1;
switch (x){
case 1: System.out.print("At 1 ");
x = 3;
case 2: System.out.print("At 2 ");
x = 1;
case default: System.out.print("At default ");
}
}
}
Which of the following is the output of the code? Select one option.
A) “At 1 At default”
B) Compile time error
C) “At 1”
D) “At 1 At 2 At default”
The answer is B. Be careful on line of code that says “case default”. The
proper code is to have only the word “default”. If the code had been correct,
the output would have been “At 1 At 2 At default”.
Q2 Unanswered.
Q3 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
System.out.print("Main method + ");
System.out.print(new Test().hypotenuse());
}
Test(){
int length = 3;
int height = 4;
}
public double hypotenuse(){
return Math.sqrt(3*3+4*4);
}
}
Which of the following is the output of the code? Select one option.
A) “Main method + “
B) “Main method + 5.0”
C) “Main method + 5”
D) “Main method + 7”
E) “Main method + 7.21”
F) Compile time error
The answer is B. The second print statement creates an anonymous instance
of the Test class and runs the hypotenuse method for that instance. Also, the
return statement does the math like this: Math.sqrt((3*3) + (4*4)).
Q3 Unanswered.
Q4 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
Test q = new Test(sb);
if(sb.toString().equals(this.toString())){
System.out.print("True");
}
}
Test(StringBuilder stringB){
stringB.append(this.toString());
}
}
True or False: The code will print “True”.
A) True
B) False
The answer is B. There is actually a compile time error. The constructor is
fine, but when the JVM evaluates sb.toString().equals(this.toString()) it sees
this.toString() as a static reference. this cannot be used in a static context
because by its nature it is referring to an instance of a class, not something at
the class level.
Q4 Unanswered.
Q5 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
char c = "a";
String s = "a";
if(c.equals(s)){
System.out.println("True");
}
}
True or false: The code will print “True”.
A) True
B) False
The answer is B. There are two problems with this code. First, char c = “a”
is not the correct way to assign a value to chars. The correct code would be
char c = ‘a’ (note the single quotes). Second, c is a primitive and thus it does
not have any methods. Therefore one cannot invoke .equals() on c. More
appropriate would be c == s, but the JVM knows that c and s are different
classes so that also causes a compile time error.
Q5 Unanswered.
Q6 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
System.out.println(args[1]);
}
}
The app is run from the command line with the following command:
java Test Print this code
Which of the following is the output of the code? Select one option.
A) Runtime Exception
B) “Test”
C) “Print”
D) “this”
E) “code”
F) “Print this code”
The answer is D. The JVM interprets spaces between Strings as being a
break between Strings. Also, remember that java starts counting at position
0, so “this” is at position 1.
Q6 Unanswered.
Q7 Answered.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
try{
ArrayList arr = new ArrayList();
arr.add("Rugby");
System.out.println(arr.get(1));
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Caught ArrayIndexOutOfBoundsException");
}catch(Exception e){
System.out.println("Caught Exception");
}
}
}
Which of the following is the output of the code? Select one option.
A) “Rugby”
B) “Caught ArrayIndexOutOfBoundsException”
C) “Caught Exception”
D) Runtime exception
The answer is C. The code throws an IndexOutOfBoundsException, not an
ArrayIndexOutOfBoundsException (although that is a valid exception).
Therefore, the exception is caught in the second catch block.
Q7 Unanswered.
Q8 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
try{
try{
ArrayList arr = new ArrayList();
arr.add("Rugby");
System.out.println(arr.get(1));
}catch(IndexOutOfBoundsException e){
throw new ArrayIndexOutOfBoundsException();
}catch(Exception e){
System.out.println("Caught Inner Exception");
}
}catch(Exception e){
System.out.println("Caught Outer Exception");
}
}
}
Which of the following is the output of the code? Select one option.
A) “Caught Inner Exception”
B) “Caught Outer Exception”
C) Runtime Exception
The answer is B. The ArrayIndexOutOfBoundsException is thrown to the
outer try-catch block. It is not merely shuffled down to the next catch block
of the inner try-catch block.
Q8 Unanswered.
Q9 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
The app is run from the command line with the following command:
java Test “Print more code”
Which of the following is the output of the code? Select one option.
A) “Test”
B) “Print”
C) “more”
D) “Print more code”
E) Runtime Exception
The answer is D. Since the class Test was run with quotes around the input,
the input (Print more code) is interpreted as one string. Thus the app prints
“Print more code”.
Q9 Unanswered.
Q10 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
if("This string" == new String("This string")){
System.out.println("This string".reverse());
}else{
System.out.println("This string".length());
}
}
}
Which of the following is the output of the code? Select one option.
A) “gnirts sihT”
B) “11”
C) “10”
D) Compile time error
The answer is D. .reverse() is a method of the StringBuilder class, not the
String class. Just as info, “This string” == new String(“This string”)
evaluates to false. The == operator evaluates whether the two objects are
literally the same object. In this case they are not because of the “new”
modifier.
Q10 Unanswered.
Q11 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
arr.add(1);
arr.add(2);
arr.add(3);
try{
//Line 1
}catch(Exception e){
System.out.println(e);
}
}
}
Which of the following can be placed at Line 1? Select two options.
A) System.out.println(arr.get(2));
B) System.out.println(arr[2]);
C) System.out.println(arr.get(5));
The answer is A, C. Even though there is a try-catch block, the compiler still
will not allow code at Line 1 that throws a compile time error. The code will
compile and execute options A and C, even though C throws a Runtime
Exception.
Q11 Unanswered.
Q12 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
if(a = b){
System.out.println("First if");
}else if(b = a){
System.out.println("Second if");
}else if(b = !a){
System.out.println("Third if");
}else if(a == !b){
System.out.println("Fourth if");
}
}
}
Which of the following is the output of the code? Select one option.
A) “First if”
B) “Second if”
C) “Third if”
D) “Fourth if”
The answer is C. if(a = b) sets a to false, so the code is really if(false) and
thus the if statement is not executed. The same holds for the second if()
statement. if(b = !a) first sets b = !a (which is the same as b = true) and then
evaluates if(true) and executes the if statement. The fourth if statement is not
entered because it is an else if() statement. Once one of the preceding if()
statements had been entered, the fourth would not execute.
Q12 Unanswered.
Q13 Answered.
Which of the following are object oriented concepts? Select three answers.
A) Designing classes to be modular and easily reusable
B) Storing object states in fields
C) Experiencing object behavior through methods
D) Having code flow in a linear fashion in an application
The answers are A, B, C. Code reuse is essential to writing good java apps.
Having code flow in linear fashion is reminiscent of older coding practices
developed before object oriented languages and makes code reuse difficult.
Q13 Unanswered.
Q14 Answered.
Which of the following code snippets compile? Select two options.
A) for(false){}
B) if(false){}
C) while(false){}
D) Boolean bool = false; while(bool){}
Q14 Unanswered.
Q15 Answered.
package Exam;
public class Test {
int size = 1;
String shape = "none";
public void Test(){
size = 10;
shape = "round";
}
public int getSize(){
return this.size;
}
public String getShape(){
return this.shape;
}
}
class Tester{
public static void main(String[] args) {
Test q = new Test();
System.out.print( q.getSize() + " " + q.getShape());
}
}
Which of the following is the output of the code when Tester is run? Select
one option.
A) “10 round”
B) “10 none”
C) “1 round”
D) “1 none”
The answer is D. public void Test looks like a constructor, but it is not. It is
actually a method because it has return type void. Therefore size still equals
1 and shape still equals none
Q15 Unanswered.
Q16 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Integer[][] arr = {{0, 3, 12},{2, (Integer) null}};
System.out.println(arr[1][1]);
}
}
Which of the following is the output of the code? Select one option.
A) Runtime Exception
B) “0”
C) “null”
D) “3”
The answer is C. The Integer class can be set to null, and it is acceptable to
have an array contain an Integer object with a null value.
Q16 Unanswered.
Q17 Answered.
Which of the following are true?
A) A class cannot contain a static reference to a non-static variable and
compile.
B) All objects referenced in a class need to have an explicitly stated import
statement
C) If there is no package statement in a class, the JVM will throw a compile
time error
D) A class can be executed by the JVM even if no parameters are passed to
the class.
The answers are A, D. A non-static, or instance level variable, cannot be
referenced unless an instance of the class exists. The reference must include
the name of the instance that the variable is a field in. As for class execution
without parameters being passed in, this happens any time the default
constructor is invoked.
Q17 Unanswered.
Q18 Answered.
What are some of the benefits of object oriented programing? Select three
answers.
A) Debugging is easier
B) Code is easily reusable
C) The code flows in a logical fashion
D) Code changes to one module do not affect another module
The answers are A, B, D. While code is not necessarily illogical, it is
difficult to follow unless one understands object instances and method calls.
With object oriented programming, one can isolate the offending code and
debug easily. Also, one can repeatedly call a class to perform similar
functions without rewriting code. Also, code changes to one class will not
affect the fields or methods of any other class.
Q18 Unanswered.
Q19 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
String s = "Team USA";
String y = "Team UK";
s.concat(y);
System.out.println(s);
modify(s,y);
System.out.println(s);
s=modify(s, y);
System.out.println(s);
}
public static String modify(String one, String two){
String mod = "";
mod = two.concat(" and") + one.substring(4);
return mod;
}
}
Which of the following is the output of the code? Select one option.
A) “Team USA” followed by “Team USA” followed by “Team UK and
Team USA”
B) “Team USA and Team UK” followed by “Team USA and Team UK”
followed by “Team UK and Team USA”
C) “Team USA” followed by “Team USA and Team UK ” followed by
“Team UK and Team USA”
D) “Team USA and Team UK” followed by “Team USA ” followed by
“Team UK and Team USA”
The answer is A. Only the statement s = modify(s,y) actually reassigns the
value in the memory space that the variable s is pointing to. In other words, s
is a new String object. s.concat(y) and modify(s,y) do not change the value
of String s because they do create a new String in memory that s is referring
to. Since strings are immutable, the changes these methods made to the value
of string s are not retained.
Q19 Unanswered.
Q20 Answered.
package Exam;
public class Test extends AA {
public static void main(String[] args) {
Test q = new Test();
q.goGet();
q.goGetter();
q.print();
}
protected void goGet(){
System.out.println("Hey!");
}
private void goGetter(){
System.out.println("Got it!");
}
static public void print(){
System.out.println("Sub Howdy!");
}
}
class AA {
public static void print(){
System.out.println("Super Howdy!");
}
}
What does the following code print? Select one of the following.
A) “Hey!, Got it!”
B) “Super Howdy!, Hey!, Got it!”
C) “Hey!” followed by “Got it!” followed by “Sub Howdy!”
D) “Hey!” followed by “Got it!” followed by “Super Howdy!”
E) Does not compile
The answer is C. The method in Test named print() shadows the static
method in AA. Also, the order of the access modifier and the static modifier
do not matter. The return type does need to be next to the method name,
however.
Q20 Unanswered.
Q21 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
switch(args[0].charAt(0)){
case TestConstants.option1: System.out.println("Option 1
Selected");
case TestConstants.option2: System.out.println("Option 2
Selected");
case TestConstants.option3: System.out.println("Option 3
Selected");
}
}
}
class TestConstants{
public static final char option1 = ‘S’;
public static final char option2 = ‘T’;
public static final char option3 = ‘X’;
}
What does the following code print when run from the command line with
the command java Test S? Select one of the following.
A) “Option 1 Selected”
B) “Option 2 Selected”
C) “Option 3 Selected”
D) Does not compile
The answer is A. This code is a good example of using a Constants class. A
Constants class helps to keep code encapsulated and promotes code reuse.
Q21 Unanswered.
Q22 Answered.
package Exam;
public class Test extends SuperTest{
public static void main(String[] args) {
Test q = new Test();
System.out.println(q.s);
}
public void run(){
s="Sub";
}
}
class SuperTest{
String s;
SuperTest(){
run();
}
public void run(){
s = "Super";
}
}
What does the following code print? Select one of the following.
A) “Super”
B) “Sub”
C) Does not compile
The answer is B. Since the subclass, Test, is what is instantiated, then it is
the methods of class test that will be called, even by the superclass’s
constructor. It is good practice to put the final modifier on any methods
called by the superclass constructor so that surprising results like this are
avoided.
Q22 Unanswered.
Q23 Answered.
package Exam;
public class Test extends SuperTest{
public static void main(String[] args) {
Test q = (Test) new SuperTest();
System.out.println(q.s);
}
public void run(){
s="This is called";
}
}
class SuperTest{
String s;
SuperTest(){
run();
}
public void run(){
s = "No, THIS is called";
}
}
What does the following code print? Select one of the following.
A) “This is called”
B) “No, THIS is called”
C) Does not compile
The answer is C. A superclass cannot be cast as a subclass. Also, Test q =
new SuperTest(); would not work. Test is the subclass and thus is “broader”
than the superclass: it will have all the fields and methods of the superclass
excluding private fields and methods.
Q23 Unanswered.
Q24 Answered.
package Exam;
public class Test extends SuperTest {
public static void main(String[] args) {
Test q = new Test();
try {
q.run();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void run() throws FileNotFoundException{
System.out.println("Overridding methods can throw more specific
exceptions");
}
}
class SuperTest{
void run() throws Exception{
System.out.println("Overridden methods must throw more general
exceptions");
}
void run(int i) throws Exception{
System.out.println("This method is overloaded, not overridden");
}
}
What does the following code print? Select one of the following.
A) "Overriding methods can throw more specific exceptions”
B) “Overridden methods must throw broader exceptions”
C) “This method is overloaded, not overridden”
D) Does not compile
The answer is A. Overriding methods can throw Exceptions that are
subclasses of the Exception class thrown by the overridden method. Also, an
overriding method can throw no exception.
Q24 Unanswered.
Q25 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
int i = 5;
while(--i>0){
System.out.println(i);
}
}
}
What is the first number printed to the console? Select one of the following.
A) “6”
B) “5”
C) “4”
D) “3”
The answer is C. - - i decrements i before evaluating.
Q25 Unanswered.
Q26 Answered.
package Exam;
public class Test{
public static void main(String[] args) {
float f = 5.0f;
Test q = new Test();
q.runInt(f);
}
public void runInt(float f){
Integer i = new Integer(new Float(f).intValue());
System.out.print(i );
}
}
What is the output of the code? Select one option.
A) “5”
B) “5.0”
C) Compile time exception
D) Runtime exception
The answer is A. Integer i = new Integer(new Float(f).intValue()); is a valid
statement to convert a Float to an Integer.
Q26 Unanswered.
Q27 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
int i = 5;
while((i+=2)<10){
System.out.println(i);
}
if(i > 10){
try {
throw new SubThrowable();
} catch (SubThrowable e) {
e.printStackTrace();
}
}
}
}
class SubThrowable extends Throwable{
SubThrowable(){
System.out.println("caught!!!");
}
}
True or false: the code compiles and prints “caught!!!” at some point.
A) True
B) False
The answer is A. Throwable can be extended and this is what a subclass
would look like. However, it is recommended to subclass Exception instead
of Throwable.
Q27 Unanswered.
Q28 Answered.
package Exam;
public class Test extends SuperTest {
public static void main(String[] args) {
Test q = new Test();
q.returnThis();
}
public Integer returnThis(){
Integer integ = 10;
System.out.println(integ);
return integ;
}
}
class SuperTest{
public Number returnThis(){
Number num = 5;
return num;
}
}
What does the following code print? Select one of the following.
A) “10”
B) “5”
C) Does not compile
The answer is A. Overriding classes can return objects that are subclasses of
the object returned by the overridden method.
Q28 Unanswered.
Q29 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Long l = 5; //Line 1
for(;l<8; l++) //Line 2
{
System.out.println(l.toString()); //Line 3
}
}
}
Which line contains an error?
A) Line 1
B) Line 2
C) Line 3
D) No error
The answer is A. The error is “cannot convert from an int to a Long”. In
other words, the compiler will not autobox 5 into a Long object. The proper
code would be Long l = 5l;
Q29 Unanswered.
Q30 Answered.
package Exam;
public class Test extends SuperTest{
static {System.out.println("Sub static block");} //Line 1
{System.out.println("Sub block");} //Line 2
public static void main(String[] args) {
Test q = new Test();
}
}
class SuperTest {
{System.out.println("Super block");} //Line 3
static {System.out.println("Super static block");} //Line 4
SuperTest(){
run1();
run2();
}
public void run1(){
{System.out.println("run1");} //Line 5
}
public static void run2(){
{System.out.println("run2");} //Line 6
}
}
In which order are the line numbers printed to the console? Select one of the
following.
A) 1, 3, 6, 2, 4, 5
B) 3, 4, 5, 6, 1, 2
C) 1, 2, 3, 4, 5, 6
D) 4, 1, 3, 5, 6, 2
E) 5, 2, 1, 3, 6, 4
The answer is D. Superclass static blocks run before subclass static blocks,
and superclass blocks run before superclass constructors or subclass blocks.
Q30 Unanswered.
Q31 Answered.
True or false? A class implementing an interface must implement all
methods of the interface.
A) True
B) False
The answer is B. While it is true that a concrete class must implement all
methods of an implemented interface, an abstract class does not have to
implement all methods of an implemented interface. However, whatever
concrete subclass that extends the abstract class will eventually have to
implement all the methods. This is because by inheriting from the abstract
class, the subclass by definition is of the type that the interface is.
For example, if abstract class Dog implements Animal, and subclass German
Shepherd extends Dog, then German Shepherd is of type Animal as well as
Dog.
Q31 Unanswered.
Q32 Answered.
True or false? A package is a folder directory that is used for organizing
classes and interfaces.
A) True
B) False
The answer is A. That is the basic definition of what a package is. If a
developer creates a new package, a new folder is created in the workspace
(folder) that the developer is working in. package com.fuel with subpackages
com.fuel.station and com.fuel.database is really just directory com/fuel with
two folders in it, /station and /database.
Q32 Unanswered.
Q33 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Race t = new Triathlon();
System.out.println(t.getName());
System.out.println(t.getNew());
}
}
class Triathlon implements Race{
String name = "triathlon";
public double getDistance(){
return distance;
}
public String getName(){
return name;
}
public String getNew(){
return "Hello World";
}
}
interface Race{
double distance = 26.2;
String name = "race";
String getName();
double getDistance();
}
What does the code print? Select one of the following.
A) “triathlon” followed by “Hello World”
B) “race” followed by “Hello World”
C) Does not compile
The answer is C. Since t has been declared as type Race, t only has access to
methods in interface Race. However, it is the methods of Triathlon that will
be called. For example, t.getName() will return “triathlon”.
Q33 Unanswered.
Q34 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
Test q = new Test();
try{
q.run();
}catch(Exception e){
System.out.println("Caught it!");
}
}
public void run() throws ExceptionR{
try{
throw new ExceptionR();
}finally{
System.out.println("Finally block");
}
}
}
class ExceptionR extends Exception{}
What does the code print? Select one of the following.
A) “Caught it!”
B) “Finally block” followed by “Caught it!”
C) Does not compile
The answer is B. Finally blocks always execute, no matter what. Even if a
method had a return statement in the catch block, the finally block would still
execute. Also, a catch block is not required after a try block, as seen in the
example above.
Q34 Unanswered.
Q35 Answered.
package Exam;
public class Num extends Small {
String s = "Very big";
final double number = 1000000000;
public static void main(String[] args) {
Small q = new Num();
System.out.println(q.s);
System.out.print(q.number);
}
}
class Small{
String s = "Very small";
final double number = .01;
}
What does the code print? Select one of the following.
A) “Very small” followed by “.01”
B) “Very big” followed by “.01”
C) “Very small” followed by “1000000000”
D) “Very big” followed by “1000000000”
The answer is A. The rule is that methods of the instantiated type are called,
while fields of the declared type are called. So in this example, q is declared
as Small but instantiated as Num, so fields of Small are called while methods
of Num are called.
Q35 Unanswered.
Q36 Answered.
What is the proper package and class name of unchecked exceptions? Select
one of the following.
A) java.lang.Exception
B) java.lang.RuntimeException
C) java.util.Exception
D) java.util.RuntimeException
The answer is B. java.lang.Exception is the only other answer that is a real
package and class. A checked exception must be explicitly handled in the
code, an unchecked exception does not. Also, unchecked exceptions can
generally be avoided. For example, IndexOutOfBoundsException can be
avoided by checking the length of an Array.
Q36 Unanswered.
Q37 Answered.
package Exam;
import java.util.ArrayList;
public class Test{
public static void main(String[] args) {
try{
arrayOops();
System.out.println("No need to catch. ");
}catch(RuntimeException e){
System.out.println("RuntimeException is catchable. ");
}finally{
System.out.println("Was it caught? ");
}
}
public static void arrayOops(){
ArrayList arr = new ArrayList();
System.out.println(arr.get(0));
}
}
What does the code print? Select one of the following.
A) “Was it caught? ”
B) “No need to catch. Was it caught? “
C) “RuntimeException is catchable. Was it caught? “
D) Code throws a runtime exception
The answer is C. Runtime exceptions are catchable, and after it is caught
then the finally block executes.
Q37 Unanswered.
Q38 Answered.
Which of the following modifiers allow all subclasses to access a method or
field? Select two of the following.
A) public
B) protected
C) no modifier (package-protected)
D) private
The answers are A, B. Package-protected allows subclasses to access a
method or field only if they are in the same package. Protected allows access
to a method or field by subclasses regardless of what package they are in.
Q38 Unanswered.
Q39 Answered.
package Exam;
public class Test {
public static void main(String[] args) {
double r = Double.valueOf("6.7");
for(int x = 0; x < r; x+=Math.round(r)-4){
System.out.print(x + “ “);
}
}
}
What does the code print? Select one of the following.
A) 0 2 4 6
B) 0 3 6
C) 1 3 5
D) 1 4 7
E) Does not compile
The answer is B. Math.round() rounds to the nearest integer, instead of
always rounding up or down. Also, Double.valueOf() is a valid method.
Q39 Unanswered.
Q40 Answered.
package Exam;
import java.util.ArrayList;
import java.util.List;
public class 1Test {
List<String> dogs = new ArrayList<String>();
public static void main(String[] args) {
String dog1 = "German Shephard";
String dog2 = "Husky";
String dog3 = "Mutt";
Character dog4 = new Character('s');
1Test q = new 1Test();
q.dogs.add(dog1);
q.dogs.add(dog2);
q.dogs.add(dog3);
for(int i = 0; i<q.dogs.size(); i++){
System.out.print(q.dogs.get(i) + ",");
}
}
}
What will the output be from the above program? Select 1 answer.
A) “German Shephard,Husky, Mutt,”
B) “German Shephard,Husky, Mutt”
C) Compile time error.
D) None of the above
The answer is C. 1Test is not a valid class name. Class names cannot start
with a number. They can contain numbers after the first character, however.
For example, Test1 would be a valid class.
Q40 Unanswered.
Q41 Answered.
package Exam;
interface TestInterface {
default void print() {
System.out.println("Default method prints!");
}
}
public class Test implements TestInterface {
public void print() {
System.out.println(“This overrides!”);
}
public static void main(String args[]) {
Test writer = new Test();
writer.print();
}
}
Which of the following is the output of the code? Select one option.
A) “Default method prints!”
B) “This overrides!”
C) Compile time error
The answer is B. An interface is not required to have an abstract method.
Also, it is acceptable to override an interface default method.
Q41 Unanswered.
Q42 Answered.
Which of the following interfaces is not directly or indirectly implemented by
ArrayList?
A) Array
B) List
C) Collection
D) Iterable
The answer is A. ArrayList extends an abstract class which implements List.
List implements Collection and Iterable.
Q42 Unanswered.
Q43 Answered.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
try{
ArrayList arr = new ArrayList();
arr.add("Index?");
System.out.println(arr.get(1));
} finally {
System.out.println("Finally Here");
}
}
}
What does the code print?
A) “Index?”
B) “Finally Here”
C) “Index?” followed by “Finally Here”
D) “Finally Here” followed by exception stack trace
E) exception stack trace
The answer is D. The finally statement executes, then an uncaught exception
is thrown.
Q43 Unanswered.
Q44 Answered.
package Exam;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<Integer, String> map = new Map<Integer, String>();
map.put(0, "element 0");
System.out.println(map.get(0));
}
}
What does the following code print?
A) “element 0”
B) null
C) Does not compile
The answer is C. Map is an interface and cannot be directly instantiated.
Instead, the code should import HashMap and read as Map<Integer, String>
map = new HashMap<Integer, String>();
Q44 Unanswered.
Q45 Answered.
Which of the following is not a concept of encapsulation?
A) Getters and Setters
B) java classes
C) extending classes
D) private modifier
The answer is C. Extending is a concept of polymorphism.
Q45 Unanswered.
Q46 Answered.
package Exam;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
try{
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(0, "element 0");
map.put(2, "element 1");
System.out.println(map.get(1));
}catch(IndexOutOfBoundsException e){
System.out.println("Incorrect implementation.");
}
}
}
What does the code print?
A) “element 0”
B) “element 1”
C) “Incorrect implementation.”
D) null
The answer is D. HashMap does not have to have a value at consecutive
keys because key may not be an integer. Also, accessing missing keys
returns a null value.
Q46 Unanswered.
Q47 Answered.
package Exam;
public class Test extends Build{
public static void main(String[] args) {
Test q = new Test ();
StringBuilder sb = new StringBuilder("");
q.display(sb);
q.display();
}
private void display(StringBuilder ss){
ss.append("California".substring(2, 2));
ss.append("California".substring(1, 3));
ss.append("California".substring(4,6));
System.out.println(ss);
}
}
class Build{
void display(){
StringBuilder ___ = new StringBuilder("");
___.append("Texas".substring(0, 2));
System.out.println(___);
}
}
What does the code print?
A) “lalifor” followed by “Tex”
B) “alfo” followed by “Te”
C) “Caif” followed by “Te”
D) Does not compile
The answer is B.
Q47 Unanswered.
Q48 Answered.
package Exam;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
int counter = 0;
for(int i = 4; i > 0; i-=2){
for(int x = 0; x < i; ++x){
++counter;
}
}
System.out.println(counter);
}
}
What does the code print?
A) 2
B) 4
C) 6
D) 8
E) 10
Q48 Unanswered.
Q49 Answered.
package Exam;
public class Test extends AnotherAbstract {
public static void main(String[] args) {}
public void methodHide(){
System.out.println("method hidden");
};
}
abstract class AnotherAbstract extends TestAbstract {
public void methodRun(){
System.out.println("method ran");
}
}
abstract class TestAbstract {
abstract void methodRun();
abstract void methodHide();
}
True or False? The code compiles.
A) True
B) False
The answer is A. Abstract class AnotherAbstract correctly extends
TestAbstract and is itself extended properly.
Q49 Unanswered.
Q50 Answered.
package Exam;
public class CaseTest{
private String justInCase(String str){
String returner = "";
switch(str) {
case "a":
returner += "a";
case "b":
returner += "b";
default:
returner += "done";
}
return returner;
}
public static void main(String []args){
CaseTest ct = new CaseTest();
System.out.println(ct.justInCase("a"));
}
}
What does the code return?
A) “a”
B) “b”
C) “done”
D) “abdone”
The answer is D. += is a way to concat and return a new string to the
variable returner. Also, this switch statement doesn’t have any break
keywords in it, so it falls through all the cases after the first one it evaluates
to true.
Q50 Unanswered.

You might also like