[go: up one dir, main page]

0% found this document useful (0 votes)
173 views64 pages

1Z0-808 - Java SE 8 Programmer

Uploaded by

Suvidha Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views64 pages

1Z0-808 - Java SE 8 Programmer

Uploaded by

Suvidha Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 64

1z0-808

Java SE 8 Programmer |
Exam A

QUESTION 1
Given:

public class App {

public static void main(String[] args) {

Boolean[] bool = new Boolean[2];

bool[0] = new Boolean (Boolean. parseBoolean ("true") );


bool[1] = new Boolean(fnull);
System.out.println(bool[0] + " " + bool[1]);

}
What is the result?

A. True false
B. True null

C. Compilation fails
D. A NullPointerException is thrown at runtime

Correct Answer: A
Section: (none)

Explanation

Explanation/Reference:

QUESTION 2
Given the code fragment:

if (aVart++ < 10) ¢


System.out.println(aVar + " Hello World!");

} else f{
System.out.println(aVar +

Hello Universe!");
}

What is the result if the integer aVar is 9?

A. Hello World!

B. Hello Universe!
C. Hello World

D. Compilation fails.

gorrect Answer: A
ection: (none)

Explanation
Explanation/Reference:
QUESTION 3
Given:

class Product {
double price;
}

public class Test {


public void updatePrice (Product product, double price) {
price = price * 2;
product.price = product.price + price;
}
public static void main(String[] args) {

Product prt = new Product {);


prt.price = 200;
double newPrice = 100;

Test t = new Test {);


t.updatePrice (prt, newPrice);
System.out.println(prt.price + " : " + newPrice);

}
What is the result?

A. 200.0: 100.0
B. 400.0 : 200.0
C. 400.0: 100.0
D. Compilation fails.

ortect newer: B
ection: (none)

Explanation

Explanation/Reference:

QUESTION 4
Given the code fragment:
1. class X {

a public void printFileContent() {


By /* code goes here */

4, throw new IOException ();

oY; }

6. }

7. public class Test {

6. public static void main(String[] args) {


Q:, X xobj] = new X();

10. xobj].printFileContent ();

HB Be }

2: 2

Which two modifications should you make so that the code compiles successfully?

O A) Replace line 8 with public static void main(String[] args) throws Except

O B) Replace line 10 with:


try {
xob].printFileContent () ;
}
catch(Exception e) { }
catch (IOException e) { }

OC) Replace line 2 with public void printFileContent() throws IOException {


OD) Replace line 4 with throw IOException ("Exception raised");

OE) At line 11, insert throw new IOException ();

Option A
Option B
. Option C
Option D
Option E

moow>

Correct Answer: AC
Section: (none)

Explanation
Explanation/Reference:

QUESTION 5
Given the code fragment:
public static void main(String[] args) {
String date = LocalDate
. parse ("2014-05-04")
. format (DateTimeFormatter.ISO_ DATE TIME);
System.out.println (date);
}

What is the result?

May 04, 2014T00:00:00.000


2014-05-04T 00:00: 00. 000

5/4/14T00:00:00.000
An exception is thrown at runtime.

OO p>

Gorrect nswer: D
ection: none)

Explanation

Explanation/Reference:

Reference:
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
(see
predefined formatters)

QUESTION 6
Given the code fragment:

public static void main(String[] args) {


StringBuilder sb = new StringBuilder (5);
String s = "";

if (sb.equals(s)) {
System.out.println("Match 1");

} else if (sb.toString().equals(s.toString())) {
System.out.printlin("Match 2");

} else {
System.out.println("No Match");

What is the result?

>

Match 1

B. Match 2

C. No Match

D. A NullPointerException is thrown at runtime.


Gorrect nswer: B
ection: (none)

Explanation

Explanation/Reference:
QUESTION 7
Given the following two classes:

public class Customer {


ElectricAccount acct = new ElectricAccount ();

public void useElectricity (double kiwh) {


acct. addKith (kWh) ;

public class ElectricAccount {


private double kWh;
private double rate = 0.07;
private double bill;

/fline nil

How should you write methods in the ElectricAccount class at line n1 so that the
member variable bill is always
equal to the value of the member variable kwh multiplied by the member variable
rate?

Any amount of electricity used by a customer (represented by an instance of the


customer class) must
contribute to the customer's bill (represented by the member variable bill) through
the method useElectricity

method. An instance of the customer class should never be able to tamper with or
decrease the value of the
member variable bill.
Cc A) public void addkKWh(double kWh) {
this.kWh += kWh;
this.bill = this.kWh*this. rate;
}

© B) public void addkWh(double kWh) {


if (kWh > O)}{
this.kwh += kwh;
this.bill = this.kWh * this.rate;

C C) private void addkWh(double kiwh) {


if (kWh > O) {
this.kWh += kWh;
this.bill = this.kWh*this. rate;

i D) public void addkWh{double kWh) {


if(kWh > 0) {
this.kWh += kWh;
setBill (this.kwWh) ;
}
}
public void setBill({double kWh) {
bill = kWh*rate;
}

A. Option A
B. Option B

C. Option C
D. Option D

Correct Answer: AC
Section: (none)

Explanation

Explanation/Reference:

QUESTION 8
Given:
public static void main(String[] args)

String ta = "A ";


ta = ta.concat("B ");
Strang tbh = "Cc ";

ta = ta.concat (tb);

ta.replace('C’, ‘"D");

ta = ta.concat (tb);

System.out.println (ta);
}

What is the result?

ABCD
ACD
ABCC
ABD
ABDC

moom>

Correct Answer: D

Section: (none)
Explanation

Explanation/Reference:

QUESTION 9
Given:

interface Readable {
public void readBook({);
public void setBookMark ();
}

abstract class Book implements Readable {

public void readBook{) { }


// line n2
}

class EBook extends Book {


public void readBook() { }
// line n4

Which option enables the code to compile?

f/f line nil

f/f line n3
c A) Replace the code fragment at line ni with:
class Book implements Readable {

© B) At line n2 insert:
public abstract void setBookMark();

c C) Replace the code fragment at line n3 with:


abstract class EBook extends Book f{

c BD) At line n4 insert:


public void setBookMark() { }

A. Option A
B. Option B

C. Option C
D. Option D

Correct Answer: C
Section: (none)

Explanation

Explanation/Reference:

QUESTION 10
Given the code fragment:

int a[] = {1l, 2, 3, 4, 5};


for (XXX) {

System.out. print (a[e]);


}

Which option can replace xxx to enable the code to print 135?

A. inte =0;e<=4; et++


B. inte=Q;e<s;e+=2
C. inte=1;e<=5;e+=1
D. inte =1;e< 5; e+ =2

Correct Answer: B
Section: (none)

Explanation
Explanation/Reference:

QUESTION 11
Given:
class CD {
Erte £y
CDf{int r){
this.r=r;
}
}

class DVD extends CD {

Ents
DVD(int r, int c) {
f/f line nl

}
}
And given the code fragment:

DVD dvd = new DVD(10,20);

Which code fragment should you use at line n1 to instantiate the dvd object
successfully?

C A) super.r = r;
this.c = c;

C B) super (r);
this (c);

© C) super(r);
this.c = ¢;

CD) thisic = r;
super (c);

A. Option A
B. Option B

C. Option C
D. Option D

Correct Answer: C
Section: (none)
Explanation

Explanation/Reference:

QUESTION 12
Given the code fragment from three files:
SalesMan. java:

package sales;
public class SalesMan { }

Product. java:

package sales.products;
public class Product { }

Market. Java:

. package market;

. /f insert code here

public class USMarket {


SalesMan sm;
Product p;

Hop WN Ee

Which code fragment, when inserted at line 2, enables the code to compile?

~ A) import sales.*;
C B) import java.sales. products.*;

© C) import sales;
import sales.products;

© D) import sales.*;
import products.*;

Cc E) import sales.*;
import sales. products.*;

A. Option A
. Option B

C. Option C
D. Option D
E. Option E

Correct Answer: E
Section: (none)

Explanation
Explanation/Reference:

QUESTION 13
Which statement best describes encapsulation?
A. Encapsulation ensures that classes can be designed so that only certain fields
and methods of an object
are accessible from other objects.

Encapsulation ensures that classes can be designed so that their methods are
inheritable.
Encapsulation ensures that classes can be designed with some fields and methods
declared as abstract.

D. Encapsulation ensures that classes can be designed so that if a method has an


argument MyType x, any
subclass of MyType can be passed to that method.

ow

Correct Answer: D
Section: (none)

Explanation
Explanation/Reference:

QUESTION 14
Given the code fragment:

String shirts[][] = new String[2] [2];


shirts[0] [0] = "red";

shirts[0)] [1] = "blue";


shirts[1][0] = "small";
shirts[1][1] = "medium";

Which code fragment prints red: blue: small: medium?


Cc A) for (int index = 1; index < 2; indext+) {
for (int idx = 1; idx < 2; idxt+t+) {
System.out.print (shirts[index] [idx] + ":");

Cc B) for (int index = 0; index < 2; ++index) {


for (int idx = 0; idx < index; ++tidx) {
System.out. print (shirts[index] [idx] + ":");

CC) for (String ¢c : colors) {


for (String s : sizes) {
System.out.printlin(s + ":");
}

CD) for (int index = 0; index < 2;) {


for (int idx = 0; idx < 2;) {
System.out. print (shirts[index] [idx] + ":");
ldxt++;

indextt;

Option A
Option B
. Option C
Option D

9Om>

Correct Answer: D
Section: (none)

Explanation
Explanation/Reference:

QUESTION 15
Given the following class:
public class CheckingAccount {
public int amount;
public CheckingAccount (int amount) {
this.amount = amount;
}
public int getAmount () {
return amount;
}
public void changeAmount (int x) {
amount += x;
}
}

And given the following main method, located in another class:

public static void main(String[] args) {

CheckingAccount acct = new CheckingAccount ( (int) (Math. random()*1000)


f/fline nil

System.out.println (acct. getAmount ());


}

Which three lines, when inserted independently at line n1, cause the program to
print a o balance?

this.amount = 0;
amount = 0;

. acct (0);
. acct.amount = 0;

acct. getAmount () = 0;
acct.changeAmount(0);

acct.changeAmount(-acct.amount);
acct.changeAmount(-acct.getAmount());

rammoo mp

Correct Answer: ACD


Section: (none)

Explanation
Explanation/Reference:

QUESTION 16
Given the code fragment:
3. public static void main(String[] args) {
4 int x = 5;

5 while (isAvailable(x)) {

6. System.out. print (x);


3
8

oO

}
10.
11. public static boolean isAvailable {int x) {
Les. return x-- > 0 ? true : false;
ESts }

Which modification enables the code to print 54321?


A. Replace line 6 with System, out. print (--x) ;
. Atline 1, insert x --;

B
C. Replace line 6 with --x; and, at line 7, insert system, out. print (x);
D. Replace line 12 With return (x > QO) ? false: true;

Correct Answer: A
Section: (none)

Explanation
Explanation/Reference:

QUESTION 17
Given the code fragment:

public class Test{

void readCard(int cardNo) throws Exception {


System.out.println("Reading Card");
}

void checkCard(int cardNo) throws RuntimeException { // line nl


System.out.println("Checking Card");
}

public static void main(String[] args) {

Test ex = new Test ()};

int cardNo = 12344;

ex. checkCard (cardNo) ; /fline n2


ex. readCard (cardNo); f/fline n3

}
What is the result?

A. Reading Card
Checking Card
B. Compilation fails only at line n1.

C. Compilation fails only at line n2.


D. Compilation fails only at line n3.

E. Compilation fails at both line n2 and line n3.

Correct Answer: D
Section: (none)
Explanation
Explanation/Reference:

QUESTION 18
Given the following main method:

public static void main(String[] args) {

int num = 5;
do f{

System.out. print (num-- +" "J;


} while (num == 0);

What is the result?

A. 543210

B. 54321

C. 421

D. 5

E. Nothing is printed

Correct Answer: D

Section: (none)
Explanation

Explanation/Reference:

QUESTION 19
Given the code fragment:

4, public static void main(String[] args) {


5 boolean opt = true;

6. switch (opt) {

7? case true:

8 System.out.print ("True");

oO

break;
10. default:
#2. System.out. print ("***");
eee }
LS: System.out.println("Done");

14. }
Which modification enables the code fragment to print TrueDone?

A. Replace line 5 With String result = "true";


Replace line 7 with case "true":

B. Replace line 5 with boolean opt = I;


Replace line 7 with case 1=

C. At line 9, remove the break statement.


D. Remove the default section.

gorrect Answer: A
ection: (none)
Explanation
Explanation/Reference:

QUESTION 20
Given:

public class Test {


public static void main(String[] args) {
String[][] chs = new String[2][];
chs[0] = new String[2];
chs[1] = new String[5];
int i = 97;

for (int a = 0; a < chs.length; at+) {


for (int b = 0; b < chs.length; bt+t)

chs[a][b] = "" + a;
L++;
}
}
for (String[] ca : chs) {
for (String c : ca) {
System.out.print(c + " ");
}

System.out.println();

What is the result?

A. 84 Bo null null null

91 98

99 100 101 102 103

. Compilation rails.

. ANullPointerException is thrown at runtime.

900
E. An ArraylndexOutOfBoundsException is thrown at runtime.

Correct Answer: A
Section: (none)

Explanation

Explanation/Reference:

QUESTION 21

Given the code fragment:

int «x = 100%

int a = xtt;

int b = ++x;

int ¢c = xtt;

Tht «ad = ta <b) °e Ka * ey Oar fh Re PPE ee

System.out.println (d) ;

What is the result?

100
101
102
103
Compilation fails

moO >

Correct Answer: E

Section: (none)
Explanation

Explanation/Reference:

QUESTION 22
Given the code fragment:

public static void main(String[] args) {


List<String> names = new ArrayList<>();
names.add{"Robb") ;
names.add ("Bran") ;
names.add ("Rick");
names.add("Bran");

if (names. remove ("Bran")) {


names.remove ("Jon");

System. out. println (names) ;


}

What is the result?


. [Robb, Rick, Bran]

B. [Robb, Rick]

C. [Robb, Bran, Rick, Bran]


D

. An exception is thrown at runtime.

Correct Answer: A
Section: (none)

Explanation

Explanation/Reference:

QUESTION 23
Given the code fragment:

public class Employee {


String name;
boolean contract;
double salary;
Employee () {
f/f line nl
}
public String toString () {
return name + ":" + contract +

:" + salary;
}
public static void main(String[] args) {
Employee e = new Employee ();
// line n2
System.out. print (6);

Which two modifications, when made independently, enable the code to print
joe:true: 100.0?
O A) Replace line n2 with:
e.name = "Joe";
e.contract = true;
e.salary = 100;

OB) Replace line n2 with:


this.name = "Joe";
this.contract = true;
this.salary = 100;

O C) Replace line ni with:


this.name = new String("Joe");
this.contract = new Boolean(true);
this.salary = new Double (100);

OD) Replace line ni with:


name = "Joe";
contract = TRUE;
salary = 100.0f;

OE) Replace line ni with:


this ("Joe", true, 100);

Option A
Option B
. Option C
Option D
Option E

moow>

Correct Answer: AC
Section: (none)

Explanation

Explanation/Reference:

QUESTION 24
Given:
class X {
static int i;
int Jj;
public static void main(String[] args) {
X xl = new X();
xX x2 = new Xj);
pad BEE Za
x1.4 4;
2... 4. 33
x2.) = 6;
System. out. println(
xl.a +" " +
Sy OS
x2.i+¢ " " 4+
OED Ys

What is the result?

A. 3456
B. 3436
C. 5456
D. 3646

ection: (none
Explanation

orrect Answer: C
§ J

Explanation/Reference:

QUESTION 25
Given:
class A f{
public A(){
System.out. print ("A ");
}
}

class B extends Af
public B{){ f/fline nl
System.out.print("B ");
}
}

class C extends Bf

public C(){ f/fline n2


System.out.print("C ");
}
public static void main(String[] args) {
Cc = new C();
}
}

What is the result?

A. CBA

B. C

Cc. ABC

D. Compilation fails at line n1 and line n2

Gorrect Answe :C
ection: (none

Explanation

Explanation/Reference:

QUESTION 26
Given the code fragment:

public static void main(String[] args) {


Stringh) err = tat, “Bw, Tere SDMpE:
for (int 1 = 0; i < arr.length; itt) {
System.out.print(arr[i] + " ");
if (arr[i].equals("C")) {
continue;
}
System.out.println("Work done");
break;
What is the result?

A. ABC Work done


B. ABC D Work done
C. A Work done

D. Compilation fails

Correct Answer: C

Section: (none)
Explanation

Explanation/Reference:

QUESTION 27
Given the code fragment:

. public class Test {


public static void main(String[] args) {
/* insert code here */
array[0)]=10;
array[1]=20;
System.out. print (array[0]+": "tarray[1]);

aosarnuwner NP

Which code fragment, when inserted at line 3, enables the code to print 10:20?

A. int array n= new int[2];


B. int[] array;

array = int[2];
C. int array = new int[2];
D. int array [2] ;

Correct Answer: B
Section: (none)

Explanation

Explanation/Reference:

QUESTION 28
Given the code from the Greeting.Java file:

public class Greeting {


public static void main(String[] args) {
System.out.println("Hello " + args[0]);
}
}

Which set of commands prints Hello Duke in the console?


Ge

Co

‘ A) javac Greeting

java Greeting Duke

B) javac Greeting.java Duke


java Greeting

C) javac Greeting. java


java Greeting Duke

CD) javac Greeting. java

java Greeting.class Duke

A. Option A
B. Option B

C. Option C
D. Option D

Correct Answer: C
Section: (none)

Explanation

Explanation/Reference:

QUESTION 29
Which three are advantages of the Java exception mechanism?

A.

mo ow

Improves the program structure because the error handling code is separated from
the normal program
function

Provides a set of standard exceptions that covers all the possible errors ;
Improves the program structure because the programmer can choose where to handle
exceptions

Improves the program structure because exceptions must be handled in the method in
which they occurred
Allows the creation of new exceptions that are tailored to the particular program
being created

Correct Answer: ACD


Section: (none)

Explanation

Explanation/Reference:
Reference: http://javajee.com/introduction-to-exceptions-in-java
QUESTION 30
Given the code fragment:

public static void main(String[] args) {

int ii = 0}

int jj = 7;

for (i1 = 0; i1 < 337 - 17 12 = ii + 2) {


System.out.print{ii + " ");
What is the result?

A. 24

B. 0246

Cc. 024

D. Compilation fails

Correct Answer: C

Section: (none)
Explanation

Explanation/Reference:

QUESTION 31
Given:

class Alpha {
int ns;
static int s;
Alpha(int ns) {
if {$e < ns) {
s = ns;
thissns = ns;
}
}
vwoid doPrint() {
System.out.println("ns = "+ ns+" 5s =" 4+ s);
}
}

And,

public class Testa {


public static void main(String[] args) {
Alpha refl = new Alpha(50);
Alpha ref2 = new Alpha(125);
Alpha ref3 = new Alpha(i100);
refl.doPrint ();
ref2.doPrint (};
ref3.doPrint ();

What is the result?


Cc A) ns = 50 s = 125
ns = 125 s = 125
ns = 100 s 125

cB) ns = 50 s = 125
ns = 125 s = 125
ns = 0s = 125

c C) ns = 50 s = SO
ns = 125 s = 125
ns = 100 s 100

cD) ns = 50s = 50
ns = 125 s = 125
ns Os = 125

Option A
Option B
. Option C
Option D

9Om>

Correct Answer: B
Section: (none)

Explanation

Explanation/Reference:

QUESTION 32
Given the code fragment:

7. StringBuilder sbl = new StringBuilder ("Duke");


8. String strl = sbl.toString();

9. f/f insert code here

10. System.out.print (strl == str2);

Which code fragment, when inserted at line 9, enables the code to print true?

String str2 = str1;


String str2 = new String (str1);

String str2 = sb1. toString ();


String str2 = "Duke";

90 wp

Correct Answer: C
Section: (none)
Explanation

Explanation/Reference:

QUESTION 33
Given the code fragment:

LocalDate datel LocalDate.nowtl);

LocalDate date2 LocalDate.of (2014, 6, 20);

LocalDate date3 = LocalDate. parse ("2014-06-20", DateTimeFormatter.ISO_DA!


System.out.println("datel = " + datel);

System.out.println("date2 "+ date2);

System.out.println("date3 "+ date3);

Assume that the system date is June 20, 2014. What is the result?

c A) datel = 2014-06-20
date2 = 2014-06-20
date3 = 2014-06-20

c B) datel = 06/20/2014
date2 = 2014-06-20
date3 = Jun 20, 2014

© C) Compilation fails.

cD) A DateParseExcpetion is thrown at runtime.

A. Option A
B. Option B

C. Option C
D. Option D

Correct Answer: D
Section: (none)
Explanation
Explanation/Reference:

QUESTION 34
Given the code fragment:

public static void main(String[] args) {

double discount = 0;
int qty = Integer. parseInt (args[0]);
f/fline ni;

And given the requirements:


If the value of the qty variable is greater than or equal to 90, discount = 0.5
= If the value of the qty variable is between 80 and 90, discount = 0.2
Which two code fragments can be independently placed at line n1 to meet the
requirements?
OA) if (qty >= 90)

{ discount

if (qty > 80 Gc& qty < 90)

OB) discount
discount

OC) discount

OD) if (qty > 80

(qty >= 90)


(qty > 80)
(qty >= 390)

discount = 0.2;
} else f{
discount = 0;
}
if (qty >= 90) {
discount = 0.5;
} else {
discount = 0;

}
OE) discount

Option A
Option B
. Option C
Option D
Option E

moow>

Correct Answer: AC
Section: (none)
Explanation

Explanation/Reference:

QUESTION 35

Given the code fragment:

(qty > 80)

&& qty < 90)


a.
a.

a.

a.

0.5% 3
{ discount = 0.2; }
5 0;
0;
5 (qty > 80)? 0.2
(qty >= 90) ? 0.

O;
public class Test {

static int count = 0;


int i = 0;

public void changeCount () {


while (i < 5) {
i++;
count++;

public static void main(String[] args)


Test checkl = new Test ();
Test check2 = new Test ();
checkl. changeCount () ;
check2. changeCount () ;
System.out. print (checkl. count +

What is the result?

A. 10:10

B. 5:5

C. 5:10

D. Compilation fails

gorrect nswer: A
ection: (none)

Explanation

Explanation/Reference:

QUESTION 36
Which three statements describe the object-oriented features of the Java language?

"MOO w>

Objects cannot be reused.


A subclass can inherit from a superclass.

Objects can share behaviors with other objects.


A package must contain more than one class.
Object is the root class of all other objects.

A main method must be declared in every class.

Correct Answer: BCF


Section: (none)

Explanation

Explanation/Reference:
+ check2. count);
QUESTION 37
Given:

public class Test {

public static void main(String[] args) {


if (args[0].equals ("Hello") ? false : true) {
System.out.printin ("Success") ;
} else {
System.out.printin ("Failure");

And given the commands:

javac Test.Java
Java Test Hello

What is the result?

A. Success
. Failure

B
C. Compilation fails.
D. An exception is thrown at runtime

Correct Answer: B
Section: (none)

Explanation

Explanation/Reference:

QUESTION 38
You are developing a banking module. You have developed a class named ccMask that
has a maskcc method.

Given the code fragment:

class CCMask {
public static String maskCC (String creditCard) {
String x = "XXXX-XXXK-XXXX-";
//line nil
}

public static void main(String[] args) {


System. out. println (maskcc ("1234-5678-9101-1121"));
}
}

You must ensure that the maskcc method returns a string that hides all digits of
the credit card number except
the four last digits (and the hyphens that separate each group of four digits).

Which two code fragments should you use at line n1, independently, to achieve this
requirement?
OF A) StringBuilder sb = new StringBuilder (creditCard) ;
sb.substring(15, 19);
return x + sb;

OB) return x + creditCard.substring(15, 19);

OC) StringBuilder sb = new StringBuilder (x);


sb.append(creditCard, 15, 19);
return sb.toString();

OD) stringBuilder sb = new StringBuilder (creditCard) ;


StringBuilder s = sb.insert(0, x);
return s.toSstring();

A. Option A
B. Option B

C. Option C
D. Option D

Correct Answer: BC
Section: (none)

Explanation
Explanation/Reference:

QUESTION 39
Given the following code:

public static void main(String[] args) {


String[] planets = {"Mercury”", "Venus", "Earth", "Mars"};

System.out.printlin(planets.length) ;
System.out.println(planets[1].length{));
}

What is the output?

7™M9O0OW >
ARORA

orrect newer: B
ection: (none)

Explanation

Explanation/Reference:
QUESTION 40
Given:

Base. java:

class Base {
public void test (){
System.out.println("Base ");
}
}

DerivedA. java:

class DerivedA extends Base {


public void test (){
System.out.println("DerivedaA ");
}
}

DerivedB. java:

class DerivedB extends DerivedA {


public void test (}{
System.out.println("DerivedB ");
}

public static void main(String[] args)

Base bl = new DerivedB{);


Base b2 = new DéerivedA();
Base b3 = new DerivedB();

bil = (Base) b3;

Base b4 = {DerivedA) b3;


bl.test ();

b4.test ();

What is the result?

A. Base
DerivedA

B. Base
DerivedB

C. DerivedB
DerivedB

D. DerivedB
DerivedA

E. Aclasscast Except ion is thrown at runtime.

Correct Answer: C
Section: (none)
Explanation

Explanation/Reference:

QUESTION 41
Given:

package pl;

public class Acc {


int p;
private int q;
protected int r;
public int s;

Test.java:

package p2;
import pl.Acc;
public class Test extends Acc {
public static void main(String[] args)
Acc obj] = new Test ();
}
}

Which statement is true?

. Both p and s are accessible by obj.


. Only s is accessible by obj.

A
B
C. Bothr and s are accessible by obj.
D. p,r, and s are accessible by obj.

Correct Answer: B
Section: (none)

Explanation

Explanation/Reference:

QUESTION 42

Given:

System.out.printin("5 + 2= " + 3 + 4);


System.out.println("5 +2= " + (3 + 4));

What is the result?


cA) S + 34
5+2 = 34

nN

N
ae
U3
ae
ney

cB) 5 +
5 +2

i}
~]

cCQ7=7
te

cD) 5 + =
B+ 2 S79

nN
3
nw

A. Option A
B. Option B

C. Option C
D. Option D

Correct Answer: B
Section: (none)
Explanation
Explanation/Reference:

QUESTION 43
Given the code fragment:

public static void main(String[] args) {

ArrayList myList = new ArrayList ();


String[] myArray;

try {
while (true) {
myList.add("My String”);
}
}
catch (RuntimeException re) |
System. out.printin("Caught a RuntimeException"”) ;
}
catch (Exception ¢) {
System.out.printin("Caught an Exception");

}
System. out. printin("Ready to use");
What is the result?

Execution terminates in the first catch statement, and caught a RuntimeException is


printed to the console.
Execution terminates In the second catch statement, and caught an Exception is
printed to the console.

A runtime error is thrown in the thread "main".

Execution completes normally, and Ready to use is printed to the console.


The code fails to compile because a throws keyword is required.

moO D>

Correct Answer: C
Section: (none)
Explanation

Explanation/Reference:

QUESTION 44
Given the code fragment:

public static void main(String[] args) {


String[] [] arr = i Saal Se a = Sa RS {chp Sie Cree a Ba
for (int i = O; i < arr.length; i++) {
for (int j = O; 9 < arr[i].length; j++) {

System.out.print(arr[i][j] + " ");


if (arr[i][j].equals("B")) ¢{
break;

}
}

continue;

What is the result?

A. ABC

B. ABCDE

C. ABDE

D. Compilation fails.

Gorrect Answe :C
ection: (none

Explanation

Explanation/Reference:

QUESTION 45
Given the code fragments:
Person. java:

public class Person {


String name;
int age;

public Person(String n; int a) {


name = n;
age = a;

public String getName() {


return name;

public int getAge({) {


return age;
}
}

Test.java:

public static void checkAge (List<Person> list, Predicate<Person> predicat


for (Person p: list) {
if (predicate.testi{p)) {
System.out.printin(p.name + " ");
}

public static void main(String[] args) {


List<Person> iList = Arrays.asList (new Person("Hank", 45),
new Person("Charlie", 40),
new Person("Smith”", 38));
/fline nl
}

Which code fragment, when inserted at line n1, enables the code to print Hank?

checkAge (iList, () -> p. get Age ( ) > 40);


checkAge(iList, Person p -> p.getAge( ) > 40);
checkAge (iList, p -> p.getAge ( ) > 40);
checkAge(iList, (Person p) -> { p.getAge() > 40; });

com>

Correct Answer: C
Section: (none)

Explanation

Explanation/Reference:
QUESTION 46
Given the code fragment:

public class App {


public static void main(String[] args) {

String stril = "Java";


String str2 = new String("java");
f/fline nl

System.out.println ("Equal");
} else {

System.out.println("Not Equal");
}

Which code fragment, when inserted at line n1, enables the App class to print
Equal?

C A) String str3 = str2;


it {stei== stirs)

Cc B) if (strl.equalsIgnoreCase (str2) )

© C) String str3 = str2;


if (stril.equals (str3))

c D) if (strl.toLowerCase({) == str2.toLowerCase ())

A. Option A
B. Option B

C. Option C
D. Option D

Correct Answer: B
Section: (none)

Explanation

Explanation/Reference:

QUESTION 47
Given the code fragment:

public static void main(String[] args) {


SPring ska = &
Sstr.trim();
System.out.printlin(str.equals("") + " " + str.isEmpty());
What is the result?

A. true true

B. true false
C. false false
D. false true

Correct Answer: C

Section: (none)
Explanation

Explanation/Reference:

QUESTION 48
Given the code fragment:

String[] strs = new String[2];

int. idx = 0;

for (String s : strs) {


strs[idx].concat(" element " + idx);
idxt++t;

for (idx = 0; idx < strs.length; idx++) {


System.out.println(strs[idx]);

What is the result?

A. Element 0
Element 1

ull lgment 4

C. Null
Null

D. A NullPointerException is thrown at runtime.

orrect newer: D
ection: none)

Explanation

Explanation/Reference:

QUESTION 49
Given:
public class SumTest {

public static void doSum(Integer x, Integer y) {


System.out.println("Integer sum is " + (x + y));

public static void doSum(double x, double y) {


System.out.printlin("double sum is " + (x + y));

public static void doSum(float x, float y) {


System.out.println("float sum is "+ (x + y));

public static void doSum(int x, int y) {


System.out.printlin("int sum is " + (x + y));

public static void main(String[] args) {


doSum(10, 20);
doSum(10.0, 20.0);

What is the result?

c A) int sum is 30
float. sum is 30.0

c B) int sum is 30
double sum is 30

© C) Integer sum is 30
double sum is 30.0

CD) Integer sum is 30


float sum is 30.0

A. Option A
B. Option B

C. Option C
D. Option D

Correct Answer: B
Section: (none)

Explanation

Explanation/Reference:
QUESTION 50

Given the definitions of the MyString class and the Test class:

MyString.java:

package pl;
class MyString {
String msg;
MyString (String msg)
this.msg = msg;
}
}

Test.java:

package pl;
public class Test {

public static void main(String[] args) {

System.out.println("Hello

"+ new StringBuilder ("Java SE 8"));

System.out.println("Hello " + new MyString("Java SE 8"));

What is the result?

Cc A) Hello Java SE 8
Hello Java SE 8

© B) Hello java.lang. StringBuilder@<<hashcodel>>


Hello pl. MyString@<<hashcode2>>

© C) Hello Java SE 8

Hello pl. MyString@<<hashcode>>

© D) Compilation fails at the test class.

A. Option A
B. Option B

C. Option C
D. Option D

Correct Answer: C
Section: (none)
Explanation

Explanation/Reference:

QUESTION 51
Given:

class Vehicle {
int x;
Vehicle (){
this(10); // line nl
}
Vehiclefint x) {
this.x = x}
}
}

class Car extends Vehicle {


int y;
Car() {
super ();
this (20); f/f line n2
}
Car(int y) {
this.y = y;
}
public String toStlring() {
return Super.x + ":" + this.y;
}
}

And given the code fragment:


And given the code fragment:

Vehicle y = new Car{);


System.out.printin(y);

What is the result?

A. 10:20
. 0:20

B
C. Compilation fails at line n1
D. Compilation fails at line n2

Correct Answer: D
Section: (none)

Explanation
Explanation/Reference:

QUESTION 52
Given:
MainTest.java:
public class MainTest {

public static void main(int[] args) {


System.out.println("int main " + args[0]);

public static void main(Object[] args) {


System.out.println("Object main " + args[0]);

public static void main(String[] args) {


System.out.printlin("String main " + args[0]);

and commands:

javac MainTest. java


java MainTest 12 3

What is the result?

A. int main 1

B. Object main 1
C. String main 1
D
E

. Compilation fails
. An exception is thrown at runtime

Correct Answer: C
Section: (none)

Explanation
Explanation/Reference:

QUESTION 53
Given the code fragment:

3. public static void main(String[] args) {


4, int, 1Var = 200;

ct. float fVar = 100.100f;

6 double dVar = 123;

7 iVar = Var;

8. fVar = iVar;

9, dVar = fVar;
10. fVar = dVar;

TT: dVar = iVar;

12. iVar = dVar;

3's, }
Which three lines fail to compile?

Line 7
Line 8
Line 9
Line 10
Line 11
Line 12

™mOO Ww >

Correct Answer: ADF

Section: (none)
Explanation

Explanation/Reference:

QUESTION 54
Given the code fragment:

public class Person {


String name;
int age = 25;

public Person(String name) {


this (3
setName (name) ;

public Person(String name, int age) {


Person (name) ;
setAge (age);

/fsetter and getter methods go here

public String show() {


return name + " "+ age + " " + number ;
}
public static void main(String[] args) {
Person pl = new Person("Jesse") ;
Person p2 = new Person("Walter"™, 52);
System.out.println(pl.show());
System.out.printlin(p2.show());

}
What is the result?
A. Jesse 25

Walter 52
B. Compilation fails only at line n4

/fline nl

//line n2
C. Compilation fails only at line n2
D. Compilation fails at both line n1 and line n2

Gorrect Answer: B
ection: (none)

Explanation

Explanation/Reference:

QUESTION 55
Given the code fragment:

int num[][] = new int[1] [3];


for (int 1 = O; i < num.length; it+) {
for {int 3 = O; 43 < num[i].length; j++) {
num[iJ][j] = 10;
}
}

Which option represents the state of the num array after successful completion of
the outer loop?

C A) num[0] [0]=10
num [0] [1]=10
num [0] [2]=10

© B) num[0O] [0]=10
num[1] [0]=10
num[2] [0]=10

© C) num[0] (0) =10


num[0O] [1]=0
num[0O] [2] =0

© D) num[0] [(0]=10
num[O] [1]=10
num[O)] [2]=10
num[0] [3]=10
num[1] [0]=0
num[1] [1]=0
num[1] [2] =0
num[1] [3]=0

A. Option A
B. Option B

C. Option C
D. Option D

Correct Answer: A
Section: (none)
Explanation

Explanation/Reference:

QUESTION 56
Given the following array:

int[] intArr = {8, 16, 32, 64, 128};

Which two code fragments, independently, print each element in this array?

O A) for (int i: intArr) {


System.out.print (intArr[i] +" ");
}

OB) for (int i : intarr) {


System.out.print(i +" ");
}

OC) for (int i=0 : intarr) {


System.out. print (intArr[i] +" ");
itt;

OD) for (int i=0; i < intArr.length; i++) {


System.out.print(i +" ");

OE) for (int i=0; i < intArr.length; i++) {


System.out.print (intArr[i] +" ");

OF) for (int i; i < intArr.length; i++) {


System.out.print (intArr[i] +" ");

Option A
Option B
. Option C
. Option D
. Option E
. Option F

™m™9U0ONP

Correct Answer: BE
Section: (none)

Explanation

Explanation/Reference:
QUESTION 57
Given the following code for a Planet object:

public class Planet {


public String name;
public int moons;

public Planet (String name, int moons)


this.name = name;
this.moons = moons;

}
And the following main method:

public static void main(String[] args) {


Planet[] planets = {
new Planet ("Mercury", QO),
new Planet ("Venus", QO),
new Planet ("Earth", 1),
new Planet ("Mars", 2)

}e

System.out.println(planets) ;
System.out.println(planets[2]};
System. out. println(planets[2].moons);

What is the output?

{
c A) planets
Earth
1

* B) [LPlanets. Planet; @15db9742


Earth
a

© C) [LPlanets. Planet; @15db9742


Planets. Planet @6d06d69c
1

cD) [LPlanets. Planet ;@15db9742


Planets. Planet @6d06d69c
[LPlanets.Moon; @7852e922

c E) [LPlanets. Planet; @15db9742


Venus
0

Option A
Option B
. Option C
Option D
Option E

moow>

Correct Answer: C
Section: (none)

Explanation

Explanation/Reference:

QUESTION 58
Given the code fragment:
int] array = {l, 2, 3, 4, 5};

And given the requirements:

1. Process all the elements of the array in the order of entry.

2. Process all the elements of the array in the reverse order of entry.
3. Process alternating elements of the array in the order of entry.

Which two statements are true?

Requirements 1, 2, and 3 can be implemented by using the enhanced for loop.


Requirements 1, 2, and 3 can be implemented by using the standard for loop.

Requirements 2 and 3 CANNOT be implemented by using the standard for loop.


Requirement 1 can be implemented by using the enhanced for loop.
Requirement 3 CANNOT be implemented by using either the enhanced for loop or the
standard for loop.

moo wp

Correct Answer: DE
Section: (none)
Explanation

Explanation/Reference:

QUESTION 59
Given the content of three files:

A.java:

public class A {
public void a() {}
int a;

B.java:

public class B {
private int doStuff() {
private int x = 100;
return x+t+;

}
C.java:

import jJava.io.*;
package pl;
class A {
public void main(String fileName) throws IOException { }

Which statement is true?

Which statement is true?

Only the A.Java file compiles successfully.


Only the B java file compiles successfully.

. Only the C.java file compiles successfully.

. The A.Java and B.java files compile successfully.


The B.java and C java files compile successfully.
The A.Java and C java files compile successfully.

T7mO0 w>

Correct Answer: E
Section: (none)
Explanation

Explanation/Reference:

QUESTION 60
Given the following class declarations:

« public abstract class Animal

* public interface Hunter

* public class Cat extends Animal implements Hunter


«public class Tiger extends Cat

Which answer fails to compile?

c A) ArrayList<Animal> myList = new ArrayList<>();


myList.add(new Tiger()};

c B) ArrayList<Hunter> myList = new ArrayList<>{();


myList.add(new Cat());

© C) ArrayList<Hunter> myList = new ArrayList<>();


myList.add{new Tiger());

C D) ArrayList<Tiger> myList = new ArrayList<>();


myList.add(new Cat ());

c E) ArrayList<Animal> myList = new ArrayLIst<>();


myList.add(new Cat ());

Option A
Option B
. Option C
Option D
Option E

moow>

Correct Answer: E
Section: (none)

Explanation

Explanation/Reference:

QUESTION 61
Given:

public class TestScope {

public static void main(String[] args) {


int varl = 200;
System. out. print (doCalec(varl1));
System.out.print(" "+varl);

static int doCalcfint vari) {


varl = varl * 2;
return varl;
What is the result?

A. 400 200
B. 200 200
C. 400 400
D. Compilation fails.

Correct Answer: A

Section: (none)
Explanation

Explanation/Reference:

QUESTION 62
Given:

public class MarkList {

int num;

public static void graceMarks (MarkList obj4)


obj4.num += 10;

public static void main(String[] args) {


MarkList obj1 = new MarkList ();
MarkList obj2 ob jl;
MarkList obj]3 = null;
obj2.num = 60;
graceMarks (obj2) ;

How many MarkList instances are created in memory at runtime?

9085}
RON =

gorrect Answer: A
ection: (none)

Explanation

Explanation/Reference:

QUESTION 63
Which statement is true about Java byte code?

. Itcanrun on any platform.


. Itcanrun on any platform only if it was compiled for that platform.

A
B
C. It can run on any platform that has the Java Runtime Environment.
D. Itcan run on any platform that has a Java compiler.
E. It can run on any platform only if that platform has both the Java Runtime
Environment and a Java compiler.

Correct Answer: D
Section: (none)

Explanation

Explanation/Reference:

Reference: http://www.math.uni-hamburg.de/doc/java/tutorial/getStarted/intro/
definition.htm|

Explanation:

Java bytecodes help make "write once, run anywhere" possible. You can compile your
program into bytecodes
on any platform that has a Java compiler. The bytecodes can then be run on any
implementation of the Java
VM. That means that as long as a computer has a Java VM, the same program written
in the Java
programming language can run on Windows 2000, a Solaris workstation, or on an iMac.

QUESTION 64
Given the code fragment:

public class Test {


public static void main(String[] args) {

/fline nl
switch (x) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println ("Two");
break;

Which three code fragments can be independently inserted at line nl to enable the
code to print one?

B. Sorex = 4:

C. String x = "1";

D. Long x = 1;

E. Double x = 1;

F. Integer x = new Integer ("1");

Gorrect nswer: ABF


ection: (none

Explanation

Explanation/Reference:
QUESTION 65
Given:
public class Triangle {
static double area;
int: b = 2) hn = 3;
public static void main(String[] args) {

double p,; b, h; /fline nl


if {area == 0) {
b = 3;
h = 4;
p = 0.5;
}
area = p * b * h; /fline n2
System.out.println("Area is " + area);

What is the result?

A. Area is 6.0
B. Area is 3.0

C. Compilation fails at line n1


D. Compilation fails at line n2.

Correct Answer: D
Section: (none)

Explanation

Explanation/Reference:

You might also like