0% found this document useful (0 votes)
279 views162 pages

Its Java Reviewer

The document outlines the structure and content of the 98-388 exam, which focuses on Java programming fundamentals, including data types, flow control, and debugging. It contains sample questions with correct answers and explanations for each topic covered in the exam. The exam consists of multiple-choice and coding questions designed to assess the candidate's knowledge and skills in Java programming.

Uploaded by

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

Its Java Reviewer

The document outlines the structure and content of the 98-388 exam, which focuses on Java programming fundamentals, including data types, flow control, and debugging. It contains sample questions with correct answers and explanations for each topic covered in the exam. The exam consists of multiple-choice and coding questions designed to assess the candidate's knowledge and skills in Java programming.

Uploaded by

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

98-388.

16q

Number: 98-388
Passing Score: 800
Time Limit: 120 min
File Version: 1.0

98-388

https://www.gratisexam.com/

Introduction to Programming Using Java

Sections
1. Understand Java fundamentals
2. Work with data types, variables, and expressions
3. Implement flow control
4. Compile and debug code

https://www.gratisexam.com/
Exam A

QUESTION 1
You have the following code segment. Line numbers are included for reference only.

https://www.gratisexam.com/

What is the output of line 07?

A. -44
B. -40.0
C. 40.0
D. 44.0

Correct Answer: B
Section: Understand Java fundamentals
Explanation

Explanation/Reference:

QUESTION 2
You need to analyze the following code segment. Line numbers are included for reference only.

https://www.gratisexam.com/
What is the output of line 12 when you run printInt()?

A. 0
B. 1
C. 2
D. 3

Correct Answer: A
Section: Understand Java fundamentals
Explanation

Explanation/Reference:

QUESTION 3
HOTSPOT

You are creating a Java console application.

You need to read a birthdate entered by the user.

https://www.gratisexam.com/
How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

https://www.gratisexam.com/
Correct Answer:

Section: Understand Java fundamentals


Explanation

Explanation/Reference:

https://www.gratisexam.com/
References:
https://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html

QUESTION 4
DRAG DROP

You need to evaluate the following Java program. Line numbers are included for reference only.

Which three values will be displayed in sequence? To answer, move the appropriate values from the list of values to the answer area and arrange them in the
correct order.

NOTE: Each correct selection is worth one point.

Select and Place:

https://www.gratisexam.com/
Correct Answer:

Section: Understand Java fundamentals


Explanation

Explanation/Reference:

QUESTION 5
HOTSPOT

You need to evaluate the following code. Line numbers are included for reference only.

https://www.gratisexam.com/
Use the drop-down menus to select the answer choice that answers each question based on the information presented in the code.

NOTE: Each correct selection is worth one point.

Hot Area:

https://www.gratisexam.com/
Correct Answer:

https://www.gratisexam.com/
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:

QUESTION 6
DRAG DROP

You have the following code:

https://www.gratisexam.com/
You need to create an int array named numbers initialized with num1, num2, and num3.

How should you complete the code? To answer, drag the appropriate code segment to the correct position. Each code segment may be used once, more than
once, or not at all. You may need to drag the split bar between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

Correct Answer:

Section: Work with data types, variables, and expressions


Explanation

Explanation/Reference:
Example:
int[] numbers = new int[] {num1, num2, num3}

References:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

https://www.gratisexam.com/
https://alvinalexander.com/blog/post/java/java-faq-create-array-int-example-syntax

QUESTION 7
HOTSPOT

You are writing a Java program.

The program must meet the following requirements:

Truncate firstName to its first five characters


Set output to a string that contains the firstName and the number of characters in firstName

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

https://www.gratisexam.com/
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

https://www.tutorialspoint.com/java/lang/stringbuilder_substring_end.htm

QUESTION 8
DRAG DROP

You are writing a Java method that evaluates an arithmetic formula.

The method accepts an int value named number, raises the value to the second power, and returns the negative value of the result.

How should you complete the code? To answer, drag the appropriate code segment to the correct position. Each code segment may be used once, more than
once, or not at all. You may need to drag the split bar between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

https://www.gratisexam.com/
Correct Answer:

Section: Work with data types, variables, and expressions


Explanation

Explanation/Reference:

QUESTION 9
The question requires that you evaluate the underlined text to determine if it is correct.

You should use an int data type to store the numeric value 3,000,000,000 (3 billion) so that the least amount of memory is used.

Review the underlined text. If it makes the statement correct, select “No change is needed.” If the statement is incorrect, select the answer choice that makes the
statement correct.

A. No change is needed.
B. a short
C. a byte
D. a long

https://www.gratisexam.com/
https://www.gratisexam.com/

Correct Answer: D
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:

QUESTION 10
You need to evaluate the following code segment:

What happens when the code segment is run?

A. iNum has a value of 0.


B. An exception is thrown.
C. iNum has a value of 2.
D. iNum has a value of 3.

Correct Answer: C
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:

QUESTION 11
DRAG DROP

https://www.gratisexam.com/
You are writing a Java method.

The program must meet the following requirements:

Accept a String parameter firstName


Display a welcome message that contains firstName
Ensure that the first letter of the name is capitalized, and the remaining letters are in lowercase

How should you complete the code? To answer, drag the appropriate code segment to the correct position. Each code segment may be used once, more than
once, or not at all. You may need to drag the split bar between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

Correct Answer:

https://www.gratisexam.com/
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:
References:
http://www.tutorialspoint.com/java/java_strings.htm

QUESTION 12
You work as a Java programmer.

You need to convert a numeric String to a primitive double value.

What code segment should you use?

A. Double.valueOf(numberString);
B. double.parseDouble(numberString);
C. String.parseDouble(numberString);
D. Double.parseDouble(numberString);

Correct Answer: B
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:
References:
https://www.javacodeexamples.com/convert-string-to-primitive-example/140

QUESTION 13
HOTSPOT

You are writing a Java console program. The program accepts command line arguments.

You need to ensure that the main method parses and handles each command line argument.

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

https://www.gratisexam.com/
Correct Answer:

https://www.gratisexam.com/
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:

QUESTION 14
HOTSPOT

You are writing a Java method.

https://www.gratisexam.com/
The method must meet the following requirements:

Accept a String array named entries


Iterate through entries
Stop the iteration and return false if any element has more than 10 characters
Otherwise, return true

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

https://www.gratisexam.com/
Correct Answer:

https://www.gratisexam.com/
Section: Implement flow control
Explanation

Explanation/Reference:

https://www.gratisexam.com/
QUESTION 15
DRAG DROP

Traders hires you to write a Java program to manage account openings.

To open a new account, a user must meet one the following requirements:

be over 65 years old and have a minimum annual income of 10,000


be at least 21 and have an annual income greater than 25,000

How should you complete the code? To answer, drag the appropriate operator to the correct position. Each operator may be used once, more than once, or not at
all. You may need to drag the split bar between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

Correct Answer:

https://www.gratisexam.com/
Section: Implement flow control
Explanation

Explanation/Reference:
References:
https://www.tutorialspoint.com/java/java_basic_operators.htm

QUESTION 16
The question requires that you evaluate the underlined text to determine if it is correct.

You have the following class definition:

The logError method can be invoked by code in all classes in the same package as the Logger class.

Review the underlined text. If it makes the statement correct, select “No change is needed.” If the statement is incorrect, select the answer choice that makes the
statement correct.

A. No change is needed
B. only by the Logger class
C. only by the Logger class and classes in the same package that inherit from it
D. by all classes in all packages

Correct Answer: C
Section: Compile and debug code
Explanation

Explanation/Reference:

https://www.gratisexam.com/
https://www.gratisexam.com/

https://www.gratisexam.com/
Corrections & Comments

QUESTION 1

You need to evaluate the following code segment. Line numbers are included for
reference only.

01 int a = 5;
02 int b = 10;
03 int c = ++a * b--;
04 System.out.println(c);
05 int d = a-- + ++b;
06 System.out.println(d);

Use the drop-down menus to select the answer choice that answers each question based
on the information presented in the code.

ANSWER

What is the output of line 04?


15

45

50

54

60
++a = 6 ; b-- = 10 therefore c = 6 * 10 = 60
What is the output of line 06?

13

14

15

16
a-- = 6 ; ++b = 10 therefore d = 6 + 10 = 16
Postfix vs Prefix - syntax - Is there a difference between x++ and ++x in java? - Stack
Overflow
QUESTION 2

You have the following code segment. Line numbers are included for reference only.

01 public static void main(String[] args)


02 {
03 double number = 27;
04 number %= -3d;
05 number += 10f;
06 number *= -4;
07 System.out.println(number);
08 }

ANSWER

What is the output of line 07?


A. -44
B. - 40.0
C. 40.0
D. 44.0

Explanation
number %= -3d; ------> 27%-3d = 0.0
number += 10f; -------> 0.0 + 10f = 10.0
number *= -4; = -------> 10.0 * -4 = -40.0
Java Operators (w3schools.com)

QUESTION 3

You are interviewing for a job as a Java developer.

You are presented with the following code. Line numbers are included for reference only.

01 char data1 = 65;


02 System.out.println(data1);
03
04 long data2 = 65;
05 System.out.println(data2);
06
07 float data3 = new Float("-65.0");
08 System.out.println(data3);
09
10 short data4 = new Short("65.0");
11 System.out.println(data4);
You need to evaluate what happens when the code runs.

Use the drop-down menus to select the answer choice that answers each question based
on the information presented in the code.

ANSWER

What happens when lines 01 and 02 are run?

The number 66 is displayed.

The letter ‘A’ is displayed.

An exception is thrown.
If int is initialized as char, it represents the ascii character value

What happens when lines 04 and 05 are run?

The number 66 is displayed.

The number 65.0 is displayed.

An exception is thrown.
Dapat 65 lang walang .0..
Long - Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
What happens when lines 07 and 08 are run?

The number 66 is displayed.

The number 65.0 is displayed.

An exception is thrown.
Dapat -65.0..
What happens when lines 10 and 11 are run?

The number 66 is displayed.

The number 65.0 is displayed.

An exception is thrown.
Short values don’t have decimal places

QUESTION 4

You need to evaluate the following Java program. Line numbers are included for
reference only.

01 public static void main(String[] args) {


02 double pi = Math.PI; //3.141593
03 System.out.format("Pi is %.3f%n", pi);
04 System.out.format("Pi is %.0f%n", pi);
05 System.out.format("Pi is %09f%n", pi);
06 }

ANSWER

What is the output of line 03?

Pi is 3.141593

Pi is 3.142

Pi is 3.14

Pi is 3.1
%.3f%n means that there are 3 decimal places
What is the output of line 04?
Pi is 3.141593

Pi is 0003.141593

Pi is 3

Pi is 3.
%.0f%n means that there are no decimal places
What is the output of line 05?

Pi is 3.141593

Pi is 3.1415930

Pi is 03.141593

Pi is 0000000003.141593
%09f%n means that there are 9 character including dots and spaces
6 lang talaga yung max na decimal places, if sobra yung sa format magdadagdag lang
nang magdadagdag ng 0 sa unahan

Java String format() method - javatpoint


Formatted output in Java - GeeksforGeeks

QUESTION 5

You need to analyze the following code segment. Line numbers are included for
reference only.
01 public void printInt()
02 {
03 if (true) {
04 int num = 1;
05 if (num > 0) {
06 num++;
07 }
08 }
09 int num = 1;
10 addOne (num);
11 num = num - 1;
12 System.out.println(num);
13 }
14
15 public void addOne (int num)
16 {
17 num = num + 1;
18 }
ANSWER

What is the output of line 12 when you run printInt()?

A. 0
B. 1
C. 2
Sa line 9 palang na num yung num na outside ng if{}.. Pag sa loob kasi ng {} nagdeclare
ng variable.. Within those curly braces lang magagamit.
So 1 yung original value then walang magbabago sa line kasi void yung method na yun
so in line 11 magiging 1 - 1 = 0

QUESTION 6

You write the following code:

Scanner sc = new Scanner ("1 Excellent 2 Good 3 Fair 4 Poor");


Object data1 = sc.next();
Object data2 = sc.next();
Object data3 = sc.nextInt();
Object data4 = sc.nextLine();

ANSWER

The value of data1 is

1 Excellent 2 Good 3 Fair 4 Poor

Excellent
Yung pinaka unang value sa scanner before the space is 1

The value of data2 is


Excellent

Excellent 2 Good 3 Fair 4 Poor

Good

(An empty string)


The next value in the scanner, after the first space, is Excellent
The value of data3 is
0

3
The next integer in the scanner is 2 but if hindi integer yung next magkakaron ng
InputMismatchException

1 Excellent 2 Good 3 Fair 4 Poor

Good 3 Fair 4 Poor

Fair 4 Poor

(An empty string)


If yung .next() kinukuha yung mga values within spaces.. Yung .nextLine() ay kinukuha
yung values hanggang dulo kahit may maencounter na space.. Kasama rin yung space
right after ng 2(data 3)

QUESTION 7

You are creating a Java console application.

You need to read a birth date entered by the user.

How should you complete the code? To answer, select the appropriate code segments in
the answer area.

ANSWER

import java.util.Scanner

public class DataReader


{
public static String getBirthdate()
{
System.out.println(“Enter your birthday in the format MMDDYYYY”);
Scanner sc = new Scanner(System.in) ;

String birthdate = sc.next() ;

sc.close() ;
return birthdate;
}
}
Java User Input (Scanner class) (w3schools.com)

QUESTION 8

You need to evaluate the following Java program. Line numbers are included for
reference only.

01 public class JavaProgram1


02 {
03 int x = 25;
04
05 public static void main(String[] args)
06 {
07 JavaProgram1 app = new JavaProgram1();
08 {
09 int x = 5;
10 }
11 {
12 int x = 10;
13 }
14 int x = 100;
15 System.out.println(x);
16 System.out.println(app.x);
17 }
18 public JavaProgram1()
19 {
20 int x = 1;
21 System.out.println(x);
22 }
23 }

ANSWER

Select and Place


Values —--> Answer Area
1 1

5 100

10 25

25

100
Sa line 5 yung main function which is yung nauuna laging mag run.. Sa line 7 cinall yung
constructor ng JavaProgram1 which prints 1 as seen in line 20 - 21.. Then in line 12 may
initialization ulit ng x but within curly braces siya kaya hindi siya magagamit outside
nun.. So in line 14 mangyayari yung first initialization ng x sa main method, which is 100,
na magagamit sa buong scope ng main… it was printed in line 15.. Sa line 16 naman
prinint yung value ng variable x sa JavaProgram1 class, which is 25 as seen in line 3,
with the use of the dot (.) operator

QUESTION 9

You are interviewing for a job at Adventure Works, Inc. The hiring manager asks you to
create a single console program.

The program takes multiple arguments from the command line and writes them to the
screen in the same order as they were typed on the command line.

Which three code segments should you use to develop the solution? To answer, move
the appropriate code segments from the list of code segments to the answer area and
arrange them in the correct order.

ANSWER

Select and Place


Code Segments —--------------------------------------> Answer Area
public static void main (String arguments) { public static void main (String[] args) {

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

System.out.println(args[i]); System.out.println(args[i]);
} }
} }

public static void main (String[] args) {

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

for (int i =1; i <= Integer.parseInt(args[0]); i++) {

System.out.println(arguments[i]);
}
}

Java main() method - Javatpoint - format ng main method


args.length returns how many arguments are in the args array which was used to loop
through the values and print them with the System.out.println(args[i]);.. Yung args[i]
kinukuha niya yung argument na nasa index i ng args so makukuha niya from 0 until last
index dahil sa for loop.
QUESTION 10

01 public static int fee(char model) {


02 int price = 0;
03 switch (model) {
04 case 'A':
05 price = 50;
06 break;
07 case 'T':
08 price = 20;
09 case 'C':
10 price = 5;
11 break;
12 default:
13 price = 100;
14 break;
15 }
16 return price;
17 }

ANSWER

What is the return value when mode1 has a value of 'A'?

5 20 50 100
Sabi sa switch If A daw yung value ng mode,l price = 50 then break out of the switch

What is the return value when mode1 has a value of 'T?

5 20 50 100
Sabi sa switch If T daw yung value ng model, price = 20 but walang break kaya
magpoproceed siya sa case C which overrides the value of price to 5
What is the return value when mode1 has a value of 'C?

5 20 50 100
Sabi sa switch If C daw yung value ng model price = 5 then break out of the switch
What is the return value when mode1 has any other value?

5 20 50 100
Sabi sa switch If hindi daw A,T or C yung value ng model, price = 100 then break out of
the switch
QUESTION 11

You have the following code:

int num1 = 10;


int num2 = 20;
int num3 = 30;

You need to create an int array named numbers initialized with numl, num2, and num3.

How should you complete the code? To answer, drag the appropriate code segment to
the correct position. Each code segment may be used once, more than once, or not at all.
You may need to drag the split bar between panes or scroll to view content.

ANSWER

int [] numbers = new int [] {num1, num2, num3} ;


How do I declare and initialize an array in Java? - Stack Overflow

QUESTION 12

You are creating a method that processes invoices. The invoices are contained in an
ArrayList instance. After each invoice is processed, the method must remove the invoice
from the ArrayList instance.

How should you complete the code? To answer, select the appropriate code segments in
the answer area.

NOTE: Each correct selection is worth one point.

ANSWER

public static void Process(ArrayList<String> invoices)


{
for (int i = 0 ; i < invoices. size() ; i = i + 1)
{
String invoice = invoices.get(i);
// TODO: Process the invoice
invoices.remove(i);
}
}

The loop must traverse the arraylist from index 0 until the last index.. So initialize i as 0
then get the index i until i is equal to the last index, .size() returns the size of the arraylist
which is greater than the last index but the condition uses < only kaya di lalagpas sa last
index yung pag iincrement ng 1 kada iteration.

Java For Loop (w3schools.com)


QUESTION 13

You are writing a Java method.

The method accepts a two-dimensional String array and prints the content of each array
element.

The size of each dimension of the array might be different.

How should you complete the code? To answer, select the appropriate code segments in
the answer area.

ANSWER

public void traverse(String[][] board)


{
for (int x = 0; x < board.length ; x++)
for (int y = 0; y < board[x].length ; y++)
System.out.println(board[x][y]);
}

String [][] - dual square brackets kasi two-dimensional array


board.length returns how many rows
board[x].length returns how many columns
Board[x][y] accesses the values in the row x and column y of the array

QUESTION 14

You are developing a Java program to play Tic-Tac-Toe. You define the following array to
store the state of the board:

char[][] grid = {
{ ‘-’ , ’-’ , ’x’},
{ ‘-’ , ’-’ , ’-’},
{ ‘-’ , ’o’ , ’-’}
};

Use the drop-down menus to select the answer choice that answers each question based
on the information presented in the code.

ANSWER

Which array element contains an "x"?

grid[0][2] grid[1][3] grid[2][0] grid[3][1]


Which array element contains an "o"?

grid[1][2] grid[2][1] grid[2][3] grid[3][2]

Grid[x][y] - x is the row number starting from zero and y is the column number starting
from zero

0 1 2
0 { ‘-’ , ’-’ , ’x’}
1 { ‘-’ , ’-’ , ’-’}
2 { ‘-’ , ’o’ , ’-’}

QUESTION 15

You are writing a Java program.

The program must meet the following requirements:

● Truncate firstName to its first five characters


● Set output to a string that contains the first Name and the number of characters in
firstName

How should you complete the code? To answer, select the appropriate code segments in
the answer area.

NOTE: Each correct selection is worth one point.

ANSWER

public static void main(String[] args)


{
String firstName = “Chritopher”;
firstName = firstName. substring(0,5) ;

String output = String.format(“%s is %d characters long”, firstName, firstName.


length() );

System.out.println(output);
}
substring(0,5) = the characters from index 0 to 4 (first 5 characters of the string)
%s = for strings
%d = for decimals
.length() = returns how many characters
QUESTION 16

You are writing a Java program that collects patient information and stores it in a
database.

You need to ensure that the program stores data using the least amount of memory.

Which data type should you use to complete each variable declaration? To answer, drag
the appropriate data type from the column on the left to its code segment on the right.
Each data may be used once, more than once, or not at all. You may need to drag the
split bar between panes or scroll to view content.

ANSWER

short birthYear = 1974;

boolean unknown = false;

char initial = ‘o’;

float salary = 22123.5f;

Java Data Types (w3schools.com)

QUESTION 17

You are writing a Java method that evaluates an arithmetic formula.

The method accepts an int value named number, raises the value to the second power,
and returns the negative value of the result.

How should you complete the code? To answer, drag the appropriate code segment to
the correct position. Each code segment may be used once, more than once, or not at all.
You may need to drag the split bar between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

ANSWER

public static int negativeSquare(int number){


return - (number * number);
};

You must return the negative value of the squared number


We obtained that by negating the product of number multiplied to number
PMDmodAS - Parentheses, Multiplication or Division, Modulo, Addition or Subtraction

Java Operators (w3schools.com)


QUESTION 18

You are creating a method that converts a string representation of a number into an
actual number.

The numbers passed into the method include whole numbers and fractional numbers.

How should you complete the code? To answer, select the appropriate code segments in
the answer area.

ANSWER

public void convertStringToNumber(String numberAsString)


{
double number = Double . parseDouble (numberAsString);
System.out.println(number);
}

We used the data type double because it is possible to have fractional numbers which
are decimals.
Double parseDouble() method in Java with examples - GeeksforGeeks

QUESTION 19

You attend an interview for a job as a Java programmer.

You need to declare a two by three array of the double type with initial values.

How should you complete the code? To answer, drag the appropriate code segment to
the correct location. Each code segment may be used once, more than once, or not at all.
You may need to drag the split bar between panes or scroll to view content.

ANSWER

double [][] maxArray = {{ 0.77, 3.4, 55 },{ 2.2, .045,, 2 }};

Multidimensional Arrays in Java - GeeksforGeeks


QUESTION 20

You are interviewing for a job as a Java developer. You need to evaluate the following
code. Line numbers are included for reference only.

01 String s1 = "Hello World";


02 String s2 = "Hello World";
03 String s3 = s2;
04
05

For each of the following statements, select Yes if the statement is true. Otherwise, select
No.

ANSWER

Yes No

s1 and s2 refer to the same object memory.

s2 and s3 refer to the same object memory.

A different string can be assigned to s1 on line 04.

A different string can be assigned to s1 on line 05.

1. Yes kasi same silang Hello World


2. Yes same string object and same memory
3. Yes pede pa ma override like pedeng lagyan ng s1 = “Goodbye World”;
4. Yes pede pa ma override like pedeng lagyan ng s1 = “Goodbye World”;
QUESTION 21
The question requires that you evaluate the underlined text to determine if it is correct.

You should use an int data type to store the numeric value 3,000,000,000 (3 billion) so
that the least amount of memory is used.

Review the underlined text. If it makes the statement correct, select "No change is
needed." If the statement is incorrect, select the answer choice that makes the statement
correct.

ANSWER

A. No change is needed.
B. a short
C. a byte
D. a long

EXPLANATION

byte = -128 to 127 (8-bit)


short = -32768 to 32767 (16-bit)
int = -2147483648 to 2147483647 (32-bit)
long = -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (64-bit)

https://www.w3schools.com/java/java_data_types.asp

QUESTION 22

You need to evaluate the following code segment:

double dNum = 2.667;


int iNum = 0;
iNum = (int)dNum;

ANSWER

What happens when the code segment is run?

A. i Num has a value of 0.


B. An exception is thrown.
C. iNum has a value of 2.
D. iNum has a value of 3.
QUESTION 23

You are writing a Java method.

The program must meet the following requirements:

● Accept a String parameter first Name


● Display a welcome message that contains first Name
● Ensure that the first letter of the name is capitalized, and the remaining letters are
in lowercase

How should you complete the code? To answer, drag the appropriate code segment to
the correct position. Each code segment may be used once, more than once, or not at all.
You may need to drag the split bar between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

ANSWER

public String showGreeting(String firstName)


{
String welcomeMsg = “Welcome, “;
welcomeMsg += firstName. substring(0,1). toUpperCase () + firstName.
substring(1). toLowerCase();

return welcomeMsg;
}

EXPLANATION

Method overload
.substring(start index) simula start index (inclusive) hanggang dulo
.substring(start index, end index) simula start index (inclusive) hanggang bago mag end
index (exclusive)

Java - String substring() Method example (beginnersbook.com)

QUESTION 24

You work as a Java programmer.

You need to convert a numeric string to a primitive double value.

What code segment should you use?


ANSWER

A. Double.valueOf (numberString);
B. double.parseDouble (numberString);
C. String.parseDouble (numberString);
D. Double.parseDouble (numberString);

EXPLANATION

parseDouble() returns a primitive double value. valueOf() returns an instance of the


wrapper class Double. SOURCE

Double parseDouble() method in Java with examples - GeeksforGeeks

QUESTION 25

Your instructor asks you to evaluate four arithmetic code segments.

What is the value of each code segment? To answer, drag the appropriate value from the
column on the left to its code segment on the right. Each data may be used once, more
than once, or not at all. You may need to drag the split bar between panes or scroll to
view content.

ANSWER

CHOICES:

0 1 2 2.5 15 19 26 30

ANSWER AREA:

(2 + 3) * 4 - 1 19

4*4+2*5 26

8*2%3 1

5/2+4%2 2

EXPLANATION

PMDmodAS - Parentheses, Multiplication or Division, Modulo, Addition or Subtraction


QUESTION 26

You are writing a Java console program. The program accepts command line arguments.

You need to ensure that the main method parses and handles each command line
argument.

How should you complete the code? To answer, select the appropriate code segments in
the answer area.

NOTE: Each correct selection is worth one point.

ANSWER

public static void main(String [] args)


Dropdown choices

ArrayList String String[]

{
for (int i = 0; i < args.length ; i++)
Dropdown choices

args[i] args[0] args.length args.length - 1 args.length + 1

{
handleArgument(args[i]);
Dropdown choices

args[i] args[0] args.charAt(i)

}
}

EXPLANATION

args.length - returns length of args.. Hindi na kailangan ng -1 kasi < lang naman so it
won’t encounter an IndexOutOfBounds

args[i] (whole argument)


args.charAt(i) (1 character lang)

SOURCE
QUESTION 27

You work as an intern Java programmer at Adventure Works. Your team lead asks you to
create a method.

The method must meet the following requirements:

• Accept an int array


• Check for duplicate values in the array
• Stop the outer loop as soon as a duplicate value has been detected and return true
• Return false if all values in the array are unique

How should you complete the code? To answer, select the appropriate code segments in
the answer area.

NOTE: Each correct selection is worth one point.

ANSWER

public static boolean duplicate(int[] array) {

Boolean isDuplicate = false;


for (int x = 0; x <= array.length-2; x++) {

for (int y = x + 1; y < array.length; y++)


if (array[x] == array[y])
isDuplicate = true;
if (isDuplicate)
break;
}
return isDuplicate;
}

EXPLANATION

Since <= may minus 1 dapat pero hanggang 2nd to the last index lang kailangan kasi
yung value ng last index ay nacompare na sa lahat pagdating sa second to the last index
so -2 yung mas efficient.. gets ba hahahaha
QUESTION 28

You are interviewing for a job as a Java developer. You need to demonstrate your
understanding of switch statements.

For each of the following code segments, select Yes if the code segment can be changed
to a switch statement with up to three case statements. Otherwise, select No.

NOTE: Each correct selection is worth one point.

ANSWER

Yes No

if (age >= 25){


discount = 0.50;
} else if (age >= 21) {
discount = 0.25;
} else {
discount = 0.0;
}

if (grade == “A”){
message = “Exceeds Standards”;
} else if (grade == “B”) {
message = “Meets Standards”;
} else {
message = “Needs Improvement”;
}

if (gpa == 4.0){
priority = 1;
} else if (gpa >= 3.0) {
priority = 2;
} else if (gpa >= 2.5) {
priority = 3;
}

EXPLANATION
Ginagamit lang switch statement if alam mo exactly yung possible inputs so dapat ==
langs (dapat yung condition specific character or value hindi yung range)

Java Switch (w3schools.com)


QUESTION 29

You are writing a Java method.

The method must meet the following requirements:

• Accept a String array named entries


• Iterate through entries
• Stop the iteration and return false if any element has more than 10 characters
• Otherwise, return true

How should you complete the code? To answer, select the appropriate code segments in
the answer area.

ANSWER

public boolean validateEntries(String[] entries) {


boolean allValidEntries = true;
for (String entry : entries){
if (entry.length() > 10) {
allValidEntries = false;
break;
}
}
return allValidEntries;
}

EXPLANATION
Enhanced For Loop/For-Each Loop: Java for-each Loop (With Examples)
(programiz.com)

QUESTION 30

Traders hires you to write a Java program to manage account openings.

To open a new account, a user must meet one the following requirements:

• be over 65 years old and have a minimum annual income of 10,000


• be at least 21 and have an annual income greater than 25,000

How should you complete the code? To answer, drag the appropriate operator to the
correct position. Each operator may be used once, more than once, or not at all. You may
need to drag the split bar between panes or scroll to view content.

ANSWER

if (num > 65 && income >= 10000 || age >= 21 && income > 25000){
System.out.println(“Approved”);
}
else {
System.out.println(“Declined”);
}

EXPLANATION
Java operators: Java Operators (w3schools.com)

QUESTION 31

You are writing a Java method named count down.

The method must meet the following requirements:

• Accept an int parameter named start


• Display all numbers from start to zero in decrements of one

ANSWER

public static void countdown(int start) {


for (int i = start; i >= 0; - - i ){
System.out.println(i);
}
}

EXPLANATION
From start to zero (included yung zero) kaya >=
For loop syntax: Java For Loop (w3schools.com)

QUESTION 32

You work as a Java programmer. A member of the team creates the following program.
Line numbers are included for reference only.

01 public static void main(String[] args) {


02 int timer = 60;
03
04 while (timer => 0)
05 {
06 if (timer = 0)
07 break;
08 else
09 {
10 System.out.println("The timer is counting down...");
11 timer++;
12 {
13 }
14 }
The program is supposed to display a message to the console while it counts down from
60. The method does not work as intended.

ANSWER

public static void main(String[] args){


int timer = 60;
while (timer >= 0){
if (timer == 0){
break;
}
else{
System.out.println(“The timer is counting down…”);
timer - - ;
}
}
}

EXPLANATION
Java operators: Java Operators (w3schools.com)

QUESTION 33

You are writing a Java console program.

The program must meet the following requirements:

• Read a line of text entered by the user


• Print each word in the text to the console on a separate line

How should you complete the code? To answer, select the appropriate code segments in
the answer area.

ANSWER

java.util.Scanner input = new java.util.Scanner (System.in);


while (input.hasNext())
{
System.out.println(input.next());
}

EXPLANATION
Usually, iniimport yung Scanner utility sa taas (import java.util.Scanner;) pero pede ring
hindi na by typing java.util.Scanner instead na Scanner lang pag ginagamit yung utility

.hasNext() returns boolean values (either true or false) kaya siya lang pede na sagot
among the choices
System.out.println kasi sabi separate line
.next() - returns the next value ng scanner (input)
QUESTION 34

You need to evaluate the following code. Line numbers are included for reference only.

01 public static void main(String[] args) {


02 int anum = 55;
03 for (int cnt = 0; cnt < 10; cnt++) {
04 add (anum);
05 {
06 System.out.println(anum);
07 }
08
09 public static void add(int anum) {
10 anum++;
11 }

ANSWER

Which statement describes the value of anum at line 05?


When cnt is 9, the value of anum is 55

When cnt is 9, the value of anum is 56

When cnt is 9, the value of anum is 64

When cnt is 9, the value of anum is 65


Hindi magbabago yung value kasi void yung add function

Which statement describes the value of anum at line 11?


When cnt is 7, the value of anum is 55

When cnt is 7, the value of anum is 56

When cnt is 7, the value of anum is 64

When cnt is 7, the value of anum is 65


Hindi magbabago yung value kasi void yung add function

Which statement describes the value of anum at line 06?


The value of cnt is 10.

The value of cnt is 11.

The value of cnt is null (?????)


Yung value ng cnt is within the for loop lang kaya pagdating sa line 6 wala siyang value
QUESTION 35

You work for Woodgrove Bank as a Java programmer.

You need to evaluate the following class. Line numbers are included for reference only.

01 public class Account {


02 protected int balance;
03 public Account() {
04 balance = 0;
05 }
06 public Account(int amount) {
07 balance = amount;
08 }
09 }

For each of the following statements, select Yes if the statement is true. Otherwise, select
No.

ANSWER

Yes No

The Account class has a single constructor.

Other classes can inherit the Account class

Line 07 is equivalent to this.balance = amount;


Java this Keyword (w3schools.com)

QUESTION 36

You have the following code segment. Line numbers are included for reference only.

01 public class Customer


02 {
03 private int id = 3;
04 public static void main(String[] args)
05 {
06 Customer customer = new Customer ();
07 id = 5;
08 showId();
09 }
10
11 protected void showId ()
12 {
13 System.out.println(id);
14 }
15 }
The code does not compile.

For each of the following statements, select Yes if the action is required to resolve the
compilation error. Otherwise, select No.

ANSWER

Yes No

Change the access modifier of the variable id to public

Change the access modifier of the showId method to public

On lines 07 and 08, add the prefix customer to id and showId()


Hindi na kailangan paltan yung mga access modifiers kasi within isang class lang nmn
sila
Para maaccess yung class variable pati class method sa main method, kailangan
gumawa ng object tapos gumamit ng dot (.) operator (customer.id =5;)

Java Dot Operator - Javatpoint

QUESTION 37

You have a Java class named InsurancePolicy.

You need to define a constant data member named RATE. The data member must be
accessible by any class without instantiating the InsurancePolicy class.

How should you complete the code? To answer, drag the appropriate code segment to
the correct position. Each code segment may be used once, more than once, or not at all.
You may need to drag the split bar between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

ANSWER

final private public super

finally protected static void

public class InsurancePolicy


{
public static final double RATE = .0642;
}
How public static final variable works in Java? Example (javarevisited.blogspot.com)
QUESTION 38

You work on a team that develops e-commerce applications. A member of the team
creates a class named DB that connects to a database. The class contains a method
named query.

You need to initiate the DB class and invoke the query method.

How should you complete the code? To answer, select the appropriate code segments in
the answer area.

ANSWER

DB db = new DB();
Dropdown choices

DB() new DB new DB()

db.query();
Dropdown choices

db.query() DB.query() query()

Java Classes and Objects (w3schools.com)

QUESTION 39

You are writing a Java class named SavingsAccount.

The class must meet the following requirements:

● Inherit from an existing class named Account


● Include a constructor that uses the base class constructor to initialize the starting
balance
● Include a substitute toString () method
How should you complete the code? To answer, select the appropriate code segments in
the answer area.
ANSWER

public class SavingsAccount extends Account {


Dropdown choices

: extends inherits implements

double rate = 0.02;


SavingsAccount(double startingBalance) {
super(startingBalance);
Dropdown choices

Account base constructor super

}
@Override
Dropdown choices

@Implements @Inject @Overload @Override

public String toString(){

Java Inheritance (Subclass and Superclass) (w3schools.com)


Annotations in Java - GeeksforGeeks

QUESTION 40

You have the following class definition:

class Logger
{
public void logError (String message)
{

}
}

The logError method can be invoked by code in all classes in the same package as the
Logger class.

Review the underlined text. If it makes the statement correct, select "No change is
needed." If the statement is incorrect, select the answer choice that makes the statement
correct.

ANSWER

A. No change is needed
B. only by the Logger class
C. only by the Logger class and classes in the same package that inherit from it
D. by all classes in all packages
QUESTION 41

You write the following Java program for Munson's Pickles and Preserves Fam. Line
numbers are included for reference only.

01 class Pickle {
02 boolean isPreserved = false;
03 private boolean iscreated = false;
04
05 void preserve() {
06 isPreserved = true;
07 }
08
09 public static void main(String[] args)
10 {
11 Pickle pickle = new pickle();
12 iscreated = true;
13 pickle.preserve;
14 }
15 }

You encounter error messages when you attempt to compile the program.

You need to ensure the program compiles successfully.

How should you complete the code? To answer, select the appropriate code segments in
the answer area.
ANSWER

class Pickle {
boolean isPreserved = false;
private boolean isCreated = false;

void preserve() {
isPreserved = true;
}

public static void main(String[] args)


{
Pickle pickle = new Pickle();
pickle.isCreated = true;
pickle.preserve();
}
}

QUESTION 42

You need to evaluate the output of the following code segment. Line numbers are
included for reference only.

01 try
02 {
03 int x = 1/ 0;
04 System.out.println("try");
05 }
06 catch (ArithmeticException ex)
07 {
08 System.out.println("catch ArithmeticException");
09 }
10 catch (Exception ex)
11 {
12 System.out.println("catch Exception");
13 }
14 finally
15 {
16 System.out.println("finally");
17 }

For each of the following outputs, select Yes if the output is displayed. Otherwise,
select No.
ANSWER

Yes No

try

catch ArithmeticException

catch Exception

finally

Explanation

Per line chinecheck yung within try and since first line palang ng try may na encounter
agad na exception which is an Arithmetic exception, hindi na aabot sa
System.out.println("try"); instead, ipiprint niya na yung System.out.println("catch
ArithmeticException"); and System.out.println("finally"); kasi kahit na may exception or
wala.. laging magrurun yung finally
Corrections & Comments

QUESTION 1

You need to create an if statement that evaluates to true when both of the following
conditions are met:

- sum is greater than or equal to num


- cnt is smaller than num

OPERATORS

&& > < >= <= == || !=

ANSWER

if (num <= sum && cnt < num)

QUESTION 2

You are evaluating the following Java method that calculates scholarships and award
amounts. Line numbers are included for reference only.

01 static double calculateAward(double gpa, int satScore, int actScore)


02 {
03 double award = 0;
04 if (gpa >= 3.7 && (satScore >= 1200 || actScore >= 26))
05 {
06 award = 30000;
07 }
08 else if (gpa >= 3.0 || satScore >= 1200 || actScore >= 26)
09 {
10 award = 15000;
11 }
12 return award;
13 }

ANSWER

What is the return value of calculateAward(3.2, 1100, 28)? 15000


What is the return value of calculateAward(2.7, 1500, 30)? 15000
What is the return value of calculateAward(3.7, 1300, 23)? 30000
QUESTION 3

You are writing a Java program to calculate the admission fee for Baldwin Museum of
Science. The standard ticket price is $10.00. The program must meet the following
requirements:

- Members are entitled to discounted tickets.


- Members over the age of 65 can visit the museum for free.
- Non-members may optionally purchase a membership in addition to their ticket.
A membership costs $40.00.

ANSWER

double calculatePrice(boolean isMember, int age, boolean buyMembership) {


double price;
if (isMember) {
if(age> 65) {
price = 0.0;
}
else{
price = 3.0;
}
}
else if(buyMembership) {
price = 50.0;
}
else {
price = 10.0;
}
return price;
}

QUESTION 4

You are writing a Java console program that accepts command line arguments.
The program needs to display each command line argument on a separate line.

ANSWER

public static void main(String[] args) {


for (String arg : args)
{
System.out.println(arg);
QUESTION 5

You are writing a Java method. The method must meet the following requirements:

- Accept an int parameter named age


- Assign the classification “senior” if age is 65 or higher
- Assign the classification “adult” if age is 20 or higher, but under 65
- Otherwise, assign the classification “youth”

ANSWER

public static String ageClassification(int age) {


String classification;

if (age >= 65)


classification = “senior”
else if (age >= 20)
classification = “adult”;
else
classification = “youth”;

return classification;
}

QUESTION 6

You work for Humongous Insurance as a Java programmer.


Insurance policy premiums are stored in a double array named allPremiums.
You need to write code to increase the premium of all policies by 25%.

: ; do for instanceof while

ANSWER

for (double premium : allPremiums) {


premium = premium * 1.25;
}
QUESTION 7

You need to refactor the following code without changing its functionality.
Line numbers are included for reference only.

01 displayMenu();
02 for ( ; ; )
03 {
04 boolean b = makeNewTransaction();
05 if (!b)
06 break;
07 displayMenu();
08 ;

} break;

do

} do;

while (makeNewTransaction())

} while (makeNewTransaction());

continue

} continue;

ANSWER

do
{
displayMenu();
} while (makeNewTransaction())

for( ; ; ) { } // produces an infinite loop


Do while kasi sa line 1 nirun niya yung displayMenu(); kaya yung sagot dapat magrurun
muna yun kahit hindi true yung condition.
QUESTION 8

Determine if the underlined text is correct.

The following class definition exists:

public class Box;


{
protected short minBoxWidth;
protected short maxBoxWidth;
}

The minBoxWidth and maxBoxWidth data members can be accessed only by the Box
class.

ANSWER

B. only by classes in the same package and classes that inherit the Box class.

QUESTION 9

You are writing a Java class named Account.


The class has the following methods:

- A method named displayTerms that can be called from any code without
instantiating the Account class
- An instance method named addAccount that can only be accessed by classes in
the same package and by subclasses of the Account class in any package

final private protected public static

ANSWER

public class Account {


public static void displayTerms() {/* … */}
protected void addAccount(String[] data) {/* … */}
}
QUESTION 10

You need to write code to test the Rectangle class.

01 public class Rectangle{


02 private int width;
03 private int length;
04
05 Rectangle(int width, int length) {
06 this.width = width;
07 this.length = length;
08 }
09
10 public int area() {
11 return this.width * this.length;
12 }
13
14 public int getWidth() {
15 return width;
16 }
17
18 public int getLength() {
19 return length;
20 }
21 }

ANSWER

int areaNum;
Rectangle rect = new Rectangle(20,40);
areaNum = rect.area();
System.out.printf(“Width = %d Length = %d\n”, rect.getWidth(), rect.getLength());
System.out.printf(“Area is correct %b\n”, areaNum == 800);
QUESTION 11

You write the following code for the School of Fine Art.
Line numbers are included for reference only.

01 public class Student


02 {
03 public String name = “Bob”;
04
05
06 public String toString()
07 {
08 return name;
09 }
10 }

The Student class must use a custom toString method instead of the standard one.
Which annotation should you use at line 05?

ANSWER

D. @Override
The Object class has some basic methods like clone(), toString(), equals(),.. etc. The
default toString() method in Object prints “class name @ hash code”. We can override
toString() method in our class to print proper output.

Overriding toString() in Java - GeeksforGeeks

QUESTION 12

You have a Java class named Account. The constructor of the Account class accepts a
String object. You need to create a class named SavingsAccount that inherits from the
Account class.

The constructor of the SavingsAccount class must meet the following requirements:

- Accept a String parameter named name


- Pass a String value SavingsAccount to the constructor of the Account class
- Initialize the class member name with the value of the constructor parameter name

Account extends implements super this


ANSWER

public class SavingsAccount extends Account {


String name;

Public SavingsAccount(String name) {


super (“SavingsAccount”);
this.name = name;

QUESTION 13

You work on a team that develops e-commerce applications. A member of the team
creates a class named DB that connects to a database. The class contains a method
named query. You need to instantiate the DB class and invoke the query method.

ANSWER

DB db = new DB();
db.query();

QUESTION 14

You need to evaluate the following code segment.


Line numbers are included for reference only.

01 double dNum = 1.7E308;


02 float fNum;
03 int iNum = 64;
04 char letter;
05
06 letter = (char)iNum;
07 iNum = (int)dNum;
08 fNum = 89.99;
09 dNum = fNum;

ANSWER
the double value 89.99 is assigned to float variable fNum which requires explicit casting

Line 06 correctly casts an int value to a char value. YES NO


A float value can be assigned to an int variable without explicit casting. YES NO
Line 08 has a compile-time error. YES NO
The statement on line 09 requires explicit casting. YES NO
QUESTION 15
Evaluate the following code:

int x = 50;
x += 100 % 5 + 10 * 2;

What is the final value of x?

ANSWER

E. 70

QUESTION 16

You are choosing the data types for some Java literals. You need to ensure that each
literal is stored precisely and uses the least amount of memory.

Which data types should you use for each literal?

boolean byte double float int

ANSWER

double 100.45232d
float 50.256f
boolean true
byte 120 byte value range: -128 to 127
int 1200000
QUESTION 17
Initialize the following variables:

int a = 5;
double b = 3.5;
int c = 33;
float d = 0.5f;
short e = 22;

What is the value of each code segment?

ANSWER

0.175 0.5 0.7 4.0 12 22 27 33

a += 4 % 2 + e; = 27
b /= 10 * 2 = 0.175
c %= e * 2 + 1 = 33
d *= 2 + 6 % 7 = 4.0

QUESTION 18

You are writing Java code to declare and initialize data types.

ANSWER

char num1 = ‘3’;


Character num2 = new Character (num1);

In development, we come across situations where we need to use objects instead of


primitive data types. In order to achieve this, Java provides wrapper class Character for
primitive data type char.

The Character class offers a number of useful class (i.e., static) methods for
manipulating characters such as isLetter(), isDigit(), isWhitespace(), etc.

https://www.tutorialspoint.com/java/java_characters.htm
QUESTION 19

You are creating a Java method.

The method accepts a String parameter named text. It returns true if text meets any of the
following conditions:

- is null
- contains only an empty string
- contains only whitespace characters

Otherwise, the method returns false.


You create the following method declaration:

public static boolean isNullOrEmpty(String text) {

if (text == “”) return true; return false;

if (text.isEmpty()) return true; if (text.trim().isEmpty()) return true;

if (text == null) return true; return true;

ANSWER

1 if (text == null) return true;


2 if (text.isEmpty()) return true;
3 if (text.trim().isEmpty()) return true;

Program to check if a String in Java contains only whitespaces - GeeksforGeeks


QUESTION 20

You are interviewing for a job as a Java developer. You need to evaluate the following
code. Line numbers are included for reference only.

01 String s1 = “Hello World”;


02 String s2 = “Hello World”;
03 String s3 = s2;
04
05

For each of the following statements, select Yes if the statement is true. Otherwise, select
No.

ANSWER

s1 and s2 refer to the same object in memory. YES NO


s2 and s3 refer to the same object in memory. YES NO
A different string can be assigned to s1 on line 04. YES NO
A different string can be assigned to s2 on line 05. YES NO

In Java, String objects are immutable. Immutable simply means unmodifiable or


unchangeable.

Once String object is created its data or state can't be changed but a new String object is
created.

https://www.javatpoint.com/immutable-string

When compiler optimizes your string literals, it sees that both s1 and s2 have same value
and thus you need only one string object. It's safe because String is immutable in Java.

String Pool is the mechanism that all already defined string are stored in some 'pool' and
before creating new String object compiler checks if such string is already defined.

https://stackoverflow.com/questions/15805578/will-two-strings-with-same-content-be-stor
ed-in-the-same-memory-location
QUESTION 21

You need to evaluate the following Java program.


Line numbers are included for reference only.

01 public static void main(String[] args)


02 {
03 int x = 5;
04 int y = 7;
05 String value1 = “x + y = “ + x + y;
06 System.out.println(value1);
07 String value2 = null;
08 value2 = value2 + “ + x + y”;
09 System.out.println(value2);
10 String value3 = “x + y + “ + (x + y);
11 System.out.println(value3);
12 }

ANSWER

What is the output of line 06? x + y = 57


What is the output of line 09? null + x + y
What is the output of line 11? x + y + 12

QUESTION 22

You initialize the following array:

int[ ] [ ] scores = new int[ ] [ ]


{
{90, 100, 55, 78}
{100, 89, 0, 85}
{65, 92, 50, 91}
};

You need to change the score 0 to 50.

ANSWER

scores[1][2] = 50;
QUESTION 23

You have the following code:

String stringValue = “true”;

You need to convert the value represented by stringValue to a boolean or Boolean.


Which code statement should you use?

ANSWER

B. Boolean booleanValue = Boolean.parseBoolean(stringValue);

The parseBoolean() method of Boolean Class is a built in static method of the class
java.lang.Boolean which is used to convert a given string to its boolean value.

https://www.geeksforgeeks.org/boolean-parseboolean-method-in-java-with-examples/

QUESTION 24

You are writing a Java method.


The method must meet the following requirements:

- Accept a double array


- Return the largest value in the array

ANSWER

public double findMax(double arr[]) {


double max =a[0]; //start with first value

for (int = 1; i < arr.length; i++)


if (a[i] > max)
max = a[i]; //new maximum

return max;
}

find sample problem:


https://perso.ensta-paris.fr/~diam/java/online/notes-java/data/arrays/arrays-ex-max.html
QUESTION 25

You are writing a Java method that evaluates an arithmetic formula

The method accepts an int value named number, raises the value to the second power,
and returns the negative value of the result.

-1 2 number + - * ^

ANSWER

public static int negativeSquare(int number) {


return - (number * number);

QUESTION 26

You need to evaluate the following code. Line numbers are included for reference only.

01 byte value1 = 127;


02 value1++;
03 System.out.println(value1);
04 System.out.println(1.0 / 3.0);
05 System.out.println(1.0f / 3.0f);

ANSWER

What is the output of line 03? -128


What is the output of line 04? 0.3333333333333333 // double only has a max of 15
decimal places
What is the output of line 05? 0.33333334

QUESTION 27

You are writing a Java program.


You need to ensure that the program stores data precisely and uses the least amount of
memory.

Which literals should you match to each data?


False ‘c’ 234 234.55f 234.55 “c” false “234”

ANSWER

char ‘c’
short 234
boolean false
float 234.55f

QUESTION 28

You are writing a Java class to implement an Integer stack based on an ArrayList.
You need to implement the following methods:

- The push method adds an Integer to the beginning of the ArrayList


- The pop method removes an Integer from the beginning of the ArrayList and
returns the removed Integer

ANSWER

public static Integer pop(ArrayList<Integer> stack) {


int index answer
= return answer
}
public static void push(ArrayList<Integer> stack, Integer item)
{
int index answer
= answer
}

QUESTION 29

You are creating a method that processes invoices. The invoices are contained in an
ArrayList instance. After each invoice is processed, the method must remove the invoice
from the ArrayList instance.

ANSWER

public static void Process(ArrayList<String> invoices)


{
for (int i = 0; i < invoices.size(); i = i + 1)
{
String invoice = invoices.get(i);
// TODO: Process the invoice
invoices.remove(i);
}
}

QUESTION 30

You are writing a Java program.


The program must meet the following requirements:

- Obtain the numeric value of the string number


- Increase the value by 5f and assign the result to sum

ANSWER

String number = “10”;


int sum = Integer.parseInt(number) + (int)5f;
System.out.println(sum);

Integer.parseInt()
Using the Integer.parseInt() method will return the string as a primitive type int. For e.g.,

//Declaring String variable


String s="200";

//Converting String into int using Integer.parseInt()


int i=Integer.parseInt(s);

Integer.valueOf()
Integer.valueOf() is to be used when an instance of the Integer class is required. For e.g.,

//Declaring a string
String number = "10";

//converting String into Integer using Integer.valueOf() method


Integer sum = Integer.valueOf(number) + (float)5f;
QUESTION 31

You are interviewing for a job as a Java developer. You need to evaluate the following
code:

double value1 = 255.255;


int value2 = (int)value1;
Byte value3 = (byte)value2;

ANSWER

The value of value1 is 255.255


The value of value2 is 255
The value of value3 is -1

binary representation of value2 (int - 4 bytes):


0000 0000 | 0000 0000 | 0000 0000 | 1111 1111

binary representation of value3 (1 byte):


1111 1111

After two's complement:


-1

- When cast to byte, Narrowing Primitive Conversion is done which discards all but the
lower 8 bits so the value fits into the byte. Since Java uses two's complement to
represent byte, this number is considered negative since the highest bit is set.

https://stackoverflow.com/questions/55990095/what-happens-when-i-assign-value-larger-
than-byte/55990215

https://www.youtube.com/watch?v=cwfC366jFFU&ab_channel=GagMansa-LearnJava

QUESTION 32

You write the following Java statement:

int result = 3 + 5 * 3 % 2;

What is the value of result after the code runs?

ANSWER

B. 4
QUESTION 33

You are interviewing for a job as a Java developer. You need to demonstrate your
understanding of the main method.

For each of the following statements, select Yes if the statement is true. Otherwise, select
No.

ANSWER

The main method must be static because YES NO


it is run without instantiating an instance
of the class.

The main method parameter args is an array YES NO


of type String.

A Java application can only accept one YES NO


argument from the command line.

QUESTION 34

You are interviewing for a job as a Java developer. You are presented with the following
code. Line numbers are included for reference only.

01 public class ScopeTester


02 {
03 int x = 5;
04 static int y = 5;
05
06 public void test()
07 {
08 int x = 10;
09 int y = 10;
10
11 System.out.println(“x = “ + x);
12 System.out.println(“this.x = “ + this.x);
13 System.out.println(“y = “ + y);
14 System.out.println(“ScopeTester.y = “ + ScopeTester.y);
15 }
16 }
ANSWER

The output of line 11 is: x = 10


The output of line 12 is: this.x = 5
The output of line 13 is: y = 10
The output of line 14 is: ScopeTester.y = 5

QUESTION 35

You need to evaluate the following code. Line numbers are included for reference only.

01 public static void main(String[] args) {


02 int anum = 55;
03 for (int cnt = 0; cnt < 10; cnt++) {
04 add(anum);
05 }
06 System.out.println(anum);
07 }
08
09 public static void add(int anum) {
10 anum++;
11 }

ANSWER

Which statement describes the value of anum at line 05?


When cnt is 9, the value of anum is 55

When cnt is 9, the value of anum is 56

When cnt is 9, the value of anum is 64

When cnt is 9, the value of anum is 65


Hindi magbabago yung value kasi void yung add function

Which statement describes the value of anum at line 11?


When cnt is 7, the value of anum is 55

When cnt is 7, the value of anum is 56

When cnt is 7, the value of anum is 64

When cnt is 7, the value of anum is 65


Hindi magbabago yung value kasi void yung add function
Which statement describes the value of anum at line 06?
The value of cnt is 10.

The value of cnt is 11.

The value of cnt is out of scope


Yung value ng cnt is within the for loop lang kaya pagdating sa line 6 wala siyang value

QUESTION 36

You need to evaluate the following code. Line numbers are included for reference only.

01 public static void method1() {


02 int x = 1;
03 int y = 1;
04
05 for (int i = 0; i < 10; i++) {
06 x += i;
07 }
08 for (int i = 0; i < 10; i++) {
09 y += i;
10 }
11 }
12
13 public static void method2() {
14 int i = 1;
15 int sum = 0;
16
17 for (int i = 0; i < 10; i++) {
18 sum += i;
19 }
20 }

ANSWER

Both methods compile with no errors. YES NO

The first method causes a compilation error YES NO


because the variable i is declared in two
Non-nested blocks.

The second method causes a compilation error YES NO


because the variable i is declared in two
nested blocks.
QUESTION 37

You are writing a method that changes the player score in a game.
You need to evaluate the code. Line numbers are included for reference only.

01 public class score {


02 static int extra = 500;
03 public static int changeScore(int score, Boolean bonus, int extra) {
04 if (bonus == true) {
05 score += extra;
06 }
07 return score;
08 }
09
10 public static void main(String[] args) {
11 Boolean bonus = true;
12 int score = 10;
13 int newScore = changeScore(score, bonus, 100);
14 System.out.println(score);
15 System.out.println(newScore);
16 }
17 }

For each of the following statements, select Yes if the statement is true. Otherwise, select
No.

ANSWER

The value of score at line 07 is 110 YES NO


The value of bonus at line 04 is true YES NO
The value of extra at line 05 is 500 YES NO

QUESTION 38

You are writing a Java console program. The program reads a temperature on the Celsius
scale and converts it to the Fahrenheit scale.
ANSWER

import java.util.Scanner;
public class TemperatureConverter {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(“Enter a degree in Celsius: ”);
double celsius = input.nextDouble();
double fahrenheit = 1.8 * celsius + 32;
System.out.print(celsius + “ Celsius is ” + fahrenheit + “ Fahrenheit”);
}
}

QUESTION 39

You are writing a Java method.


The method must read a name from the console using a class in the java.util package.

ANSWER

import java.util Scanner;

public class Main {

public static void main(String[] args) {


Scanner input = new Scanner (System.in);
System.out.print(“Enter Your Name: ”);
String name = input.nextLine();

// TODO
}
}

QUESTION 40

You write the following code:

Scanner sc = new Scanner(“1 Excellent 2 Good 3 Fair 4 Poor”);


Object data1 = sc.next();
Object data2 = sc.next();
Object data3 = sc.nextInt();
Object data4 = sc.nextLine();

You need to determine the values of the data1, data2, data3 and data4 variables.
ANSWER
The value of data1 is 1
The value of data2 is Excellent
The value of data3 is 2
The value of data4 is Good 3 Fair 4 Poor

QUESTION 41

You are creating a Java console application for City Power & Light.
You need to define the signature of the first method that gets executed when the
application starts.

ANSWER

public static void main(String args[])


{
System.out.print(“City Power & Light”);
}

QUESTION 42

You are writing the following Java program for Munson’s Pickles and Preserves Farm.
Line numbers are included for reference only.

01 class Pickle {
02 boolean isPreserved = false;
03 private boolean isCreated = false;
04
05 void preserve() {
06 isPreserved = true;
07 }
08
09 public static void main(String[] args)
10 {
11 Pickle pickle = new pickle();
12 isCreated = true;
13 pickle.preserve;
14 }
15 }

ANSWER
01 class Pickle {
02 boolean isPreserved = false;
03 private boolean isCreated = false;
04
05 void preserve() {
06 isPreserved = true;
07 }
08
09 public static void main(String[] args)
10 {
11 Pickle pickle = new Pickle();
12 pickle.isCreated = true;
13 pickle.preserve();
14 }
15 }

QUESTION 43

You need to evaluate the following exception:

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5


at mtajava.Program.arrayDisplay(Program.java:31)
at mtajava.Program.beginProcess(Program.java:23)
at mtajava.Program.main(Program.java:18)

For each of the following statements, select Yes if the statement is True. Otherwise,
select No.

ANSWER

The root cause of the exception YES NO


is in the method beginProcess().

An error occurred on line 31. YES NO

Three methods were invoked YES NO


before the error occurred.

The stack trace indicates that YES NO


the exception was caused by
a syntax error.
QUESTION 44

You are developing an application that reads and writes files.


The application must meet the following requirements:

- If an exception occurs due to file handling errors, the exception details need to be
displayed.
- If any other exception occurs, the stack information needs to be displayed.

ANSWER

try
{
/* add logic */
}
catch (IOException e1)
{
System.out.println(e1.getMessage());
}
catch (Exception e2)
{
System.out.println(e2.printStackTrace());
}
98-388.16q

Number: 98-388
Passing Score: 800
Time Limit: 120 min
File Version: 1.0

98-388

https://www.gratisexam.com/

Introduction to Programming Using Java

Sections
1. Understand Java fundamentals
2. Work with data types, variables, and expressions
3. Implement flow control
4. Compile and debug code

https://www.gratisexam.com/
Exam A

QUESTION 1
You have the following code segment. Line numbers are included for reference only.

https://www.gratisexam.com/

What is the output of line 07?

A. -44
B. -40.0
C. 40.0
D. 44.0

Correct Answer: B
Section: Understand Java fundamentals
Explanation

Explanation/Reference:

QUESTION 2
You need to analyze the following code segment. Line numbers are included for reference only.

https://www.gratisexam.com/
What is the output of line 12 when you run printInt()?

A. 0
B. 1
C. 2
D. 3

Correct Answer: A
Section: Understand Java fundamentals
Explanation

Explanation/Reference:

QUESTION 3
HOTSPOT

You are creating a Java console application.

You need to read a birthdate entered by the user.

https://www.gratisexam.com/
How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

https://www.gratisexam.com/
Correct Answer:

Section: Understand Java fundamentals


Explanation

Explanation/Reference:

https://www.gratisexam.com/
References:
https://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html

QUESTION 4
DRAG DROP

You need to evaluate the following Java program. Line numbers are included for reference only.

Which three values will be displayed in sequence? To answer, move the appropriate values from the list of values to the answer area and arrange them in the
correct order.

NOTE: Each correct selection is worth one point.

Select and Place:

https://www.gratisexam.com/
Correct Answer:

Section: Understand Java fundamentals


Explanation

Explanation/Reference:

QUESTION 5
HOTSPOT

You need to evaluate the following code. Line numbers are included for reference only.

https://www.gratisexam.com/
Use the drop-down menus to select the answer choice that answers each question based on the information presented in the code.

NOTE: Each correct selection is worth one point.

Hot Area:

https://www.gratisexam.com/
Correct Answer:

https://www.gratisexam.com/
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:

QUESTION 6
DRAG DROP

You have the following code:

https://www.gratisexam.com/
You need to create an int array named numbers initialized with num1, num2, and num3.

How should you complete the code? To answer, drag the appropriate code segment to the correct position. Each code segment may be used once, more than
once, or not at all. You may need to drag the split bar between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

Correct Answer:

Section: Work with data types, variables, and expressions


Explanation

Explanation/Reference:
Example:
int[] numbers = new int[] {num1, num2, num3}

References:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

https://www.gratisexam.com/
https://alvinalexander.com/blog/post/java/java-faq-create-array-int-example-syntax

QUESTION 7
HOTSPOT

You are writing a Java program.

The program must meet the following requirements:

Truncate firstName to its first five characters


Set output to a string that contains the firstName and the number of characters in firstName

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

https://www.gratisexam.com/
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

https://www.tutorialspoint.com/java/lang/stringbuilder_substring_end.htm

QUESTION 8
DRAG DROP

You are writing a Java method that evaluates an arithmetic formula.

The method accepts an int value named number, raises the value to the second power, and returns the negative value of the result.

How should you complete the code? To answer, drag the appropriate code segment to the correct position. Each code segment may be used once, more than
once, or not at all. You may need to drag the split bar between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

https://www.gratisexam.com/
Correct Answer:

Section: Work with data types, variables, and expressions


Explanation

Explanation/Reference:

QUESTION 9
The question requires that you evaluate the underlined text to determine if it is correct.

You should use an int data type to store the numeric value 3,000,000,000 (3 billion) so that the least amount of memory is used.

Review the underlined text. If it makes the statement correct, select “No change is needed.” If the statement is incorrect, select the answer choice that makes the
statement correct.

A. No change is needed.
B. a short
C. a byte
D. a long

https://www.gratisexam.com/
https://www.gratisexam.com/

Correct Answer: D
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:

QUESTION 10
You need to evaluate the following code segment:

What happens when the code segment is run?

A. iNum has a value of 0.


B. An exception is thrown.
C. iNum has a value of 2.
D. iNum has a value of 3.

Correct Answer: C
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:

QUESTION 11
DRAG DROP

https://www.gratisexam.com/
You are writing a Java method.

The program must meet the following requirements:

Accept a String parameter firstName


Display a welcome message that contains firstName
Ensure that the first letter of the name is capitalized, and the remaining letters are in lowercase

How should you complete the code? To answer, drag the appropriate code segment to the correct position. Each code segment may be used once, more than
once, or not at all. You may need to drag the split bar between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

Correct Answer:

https://www.gratisexam.com/
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:
References:
http://www.tutorialspoint.com/java/java_strings.htm

QUESTION 12
You work as a Java programmer.

You need to convert a numeric String to a primitive double value.

What code segment should you use?

A. Double.valueOf(numberString);
B. double.parseDouble(numberString);
C. String.parseDouble(numberString);
D. Double.parseDouble(numberString);

Correct Answer: B
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:
References:
https://www.javacodeexamples.com/convert-string-to-primitive-example/140

QUESTION 13
HOTSPOT

You are writing a Java console program. The program accepts command line arguments.

You need to ensure that the main method parses and handles each command line argument.

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

https://www.gratisexam.com/
Correct Answer:

https://www.gratisexam.com/
Section: Work with data types, variables, and expressions
Explanation

Explanation/Reference:

QUESTION 14
HOTSPOT

You are writing a Java method.

https://www.gratisexam.com/
The method must meet the following requirements:

Accept a String array named entries


Iterate through entries
Stop the iteration and return false if any element has more than 10 characters
Otherwise, return true

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

https://www.gratisexam.com/
Correct Answer:

https://www.gratisexam.com/
Section: Implement flow control
Explanation

Explanation/Reference:

https://www.gratisexam.com/
QUESTION 15
DRAG DROP

Traders hires you to write a Java program to manage account openings.

To open a new account, a user must meet one the following requirements:

be over 65 years old and have a minimum annual income of 10,000


be at least 21 and have an annual income greater than 25,000

How should you complete the code? To answer, drag the appropriate operator to the correct position. Each operator may be used once, more than once, or not at
all. You may need to drag the split bar between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

Correct Answer:

https://www.gratisexam.com/
Section: Implement flow control
Explanation

Explanation/Reference:
References:
https://www.tutorialspoint.com/java/java_basic_operators.htm

QUESTION 16
The question requires that you evaluate the underlined text to determine if it is correct.

You have the following class definition:

The logError method can be invoked by code in all classes in the same package as the Logger class.

Review the underlined text. If it makes the statement correct, select “No change is needed.” If the statement is incorrect, select the answer choice that makes the
statement correct.

A. No change is needed
B. only by the Logger class
C. only by the Logger class and classes in the same package that inherit from it
D. by all classes in all packages

Correct Answer: C
Section: Compile and debug code
Explanation

Explanation/Reference:

https://www.gratisexam.com/
https://www.gratisexam.com/

https://www.gratisexam.com/
98-388.exam-labs.pemium.exam.42q

Number: 98-388
Passing Score: 800
Time Limit: 120 min
File Version: 1.0

98-388

Introduction to Programming Using Java

Version 1.0

Sections
1. Topic 1, Understand Java fundamentals
2. Topic 2, Work with data types, variables, and expressions
3. Topic 3, Implement flow control
4. Topic 4, Perform object-oriented programming
5. Topic 5, Compile and debug code

VCEConvert.com
Exam A

QUESTION 1
HOTSPOT

You need to evaluate the following code segment. Line numbers are included for reference only.

Use the drop-down menus to select the answer choice that answers each question based on the information
presented in the code.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

Section: Topic 1, Understand Java fundamentals


Explanation

VCEConvert.com
Explanation/Reference:

QUESTION 2
You have the following code segment. Line numbers are included for reference only.

What is the output of line 07?

A. -44
B. -40.0
C. 40.0
D. 44.0

Correct Answer: B
Section: Topic 1, Understand Java fundamentals
Explanation

Explanation/Reference:

QUESTION 3
HOTSPOT

You are interviewing for a job as a Java developer.

You are presented with the following code. Line numbers are included for reference only.

You need to evaluate what happens when the code runs.

Use the drop-down menus to select the answer choice that answers each question based on the information
presented in the code.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

Section: Topic 1, Understand Java fundamentals


Explanation

Explanation/Reference:

QUESTION 4
HOTSPOT

You need to evaluate the following Java program. Line numbers are included for reference only.

VCEConvert.com
Use the drop-down menus to select the answer choice that answers each question based on the information
presented in the code.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

VCEConvert.com
Section: Topic 1, Understand Java fundamentals
Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

QUESTION 5
You need to analyze the following code segment. Line numbers are included for reference only.

What is the output of line 12 when you run printInt()?

A. 0
B. 1
C. 2

VCEConvert.com
D. 3

Correct Answer: A
Section: Topic 1, Understand Java fundamentals
Explanation

Explanation/Reference:

QUESTION 6
HOTSPOT

You write the following code:

You need to determine the values of the data1, data2, data3, and data4 variables.

Use the drop-down menus to select the answer choice that answers each question based on the information
presented in the code.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

VCEConvert.com
Section: Topic 1, Understand Java fundamentals
Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

QUESTION 7
HOTSPOT

You are creating a Java console application.

You need to read a birthdate entered by the user.

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

VCEConvert.com
Section: Topic 1, Understand Java fundamentals
Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html

QUESTION 8
DRAG DROP

You need to evaluate the following Java program. Line numbers are included for reference only.

VCEConvert.com
Which three values will be displayed in sequence? To answer, move the appropriate values from the list of
values to the answer area and arrange them in the correct order.

NOTE: Each correct selection is worth one point.

Select and Place:

Correct Answer:

Section: Topic 1, Understand Java fundamentals


Explanation

Explanation/Reference:

QUESTION 9
DRAG DROP

VCEConvert.com
You are interviewing for a job at Adventure Works, Inc. The hiring manager asks you to create a single console
program.

The program takes multiple arguments from the command line and writes them to the screen in the same
order as they were typed on the command line.

Which three code segments should you use to develop the solution? To answer, move the appropriate code
segments from the list of code segments to the answer area and arrange them in the correct order.

Select and Place:

Correct Answer:

Section: Topic 1, Understand Java fundamentals


Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/tutorial/getStarted/cupojava/netbeans.html

QUESTION 10
HOTSPOT

You need to evaluate the following code. Line numbers are included for reference only.

VCEConvert.com
Use the drop-down menus to select the answer choice that answers each question based on the information
presented in the code.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

VCEConvert.com
Section: Topic 2, Work with data types, variables, and expressions
Explanation

Explanation/Reference:

QUESTION 11
DRAG DROP

You have the following code:

You need to create an int array named numbers initialized with num1, num2, and num3.

How should you complete the code? To answer, drag the appropriate code segment to the correct position.
Each code segment may be used once, more than once, or not at all. You may need to drag the split bar
between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

VCEConvert.com
Correct Answer:

Section: Topic 2, Work with data types, variables, and expressions


Explanation

Explanation/Reference:
Example:
int[] numbers = new int[] {num1, num2, num3}

References:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

https://alvinalexander.com/blog/post/java/java-faq-create-array-int-example-syntax

QUESTION 12
HOTSPOT

You are creating a method that processes invoices. The invoices are contained in an ArrayList instance.
After each invoice is processed, the method must remove the invoice from the ArrayList instance.

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

Section: Topic 2, Work with data types, variables, and expressions


Explanation

Explanation/Reference:
References:
https://beginnersbook.com/2013/12/java-arraylist/

QUESTION 13
HOTSPOT

You are writing a Java method.

The method accepts a two-dimensional String array and prints the content of each array element.

The size of each dimension of the array might be different.

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

Section: Topic 2, Work with data types, variables, and expressions

VCEConvert.com
Explanation

Explanation/Reference:
References:
https://www.tutorialgateway.org/two-dimensional-array-in-java/

QUESTION 14
HOTSPOT

You are developing a Java program to play Tic-Tac-Toe. You define the following array to store the state of the
board:

Use the drop-down menus to select the answer choice that answers each question based on the information
presented in the code.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

VCEConvert.com
Section: Topic 2, Work with data types, variables, and expressions
Explanation

Explanation/Reference:

QUESTION 15
HOTSPOT

You are writing a Java program.

The program must meet the following requirements:

Truncate firstName to its first five characters


Set output to a string that contains the firstName and the number of characters in firstName

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

VCEConvert.com
Section: Topic 2, Work with data types, variables, and expressions
Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

https://www.tutorialspoint.com/java/lang/stringbuilder_substring_end.htm

QUESTION 16
DRAG DROP

You are writing a Java program that collects patient information and stores it in a database.

You need to ensure that the program stores data using the least amount of memory.

Which data type should you use to complete each variable declaration? To answer, drag the appropriate data
type from the column on the left to its code segment on the right. Each data may be used once, more than
once, or not at all. You may need to drag the split bar between panes or scroll to view content.

Select and Place:

Correct Answer:

Section: Topic 2, Work with data types, variables, and expressions


Explanation

VCEConvert.com
Explanation/Reference:
References:
http://www.tutorialspoint.com/java/java_basic_datatypes.htm

QUESTION 17
DRAG DROP

You are writing a Java method that evaluates an arithmetic formula.

The method accepts an int value named number, raises the value to the second power, and returns the
negative value of the result.

How should you complete the code? To answer, drag the appropriate code segment to the correct position.
Each code segment may be used once, more than once, or not at all. You may need to drag the split bar
between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

Correct Answer:

Section: Topic 2, Work with data types, variables, and expressions


Explanation

Explanation/Reference:

QUESTION 18
HOTSPOT

You are creating a method that converts a string representation of a number into an actual number.

The numbers passed into the method include whole numbers and fractional numbers.

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

Section: Topic 2, Work with data types, variables, and expressions


Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/tutorial/java/data/converting.html

QUESTION 19
DRAG DROP

You attend an interview for a job as a Java programmer.

You need to declare a two by three array of the double type with initial values.

How should you complete the code? To answer, drag the appropriate code segment to the correct location.
Each code segment may be used once, more than once, or not at all. You may need to drag the split bar
between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

VCEConvert.com
Correct Answer:

Section: Topic 2, Work with data types, variables, and expressions


Explanation

Explanation/Reference:
References:
https://www.dummies.com/programming/java/java-use-arrays-with-two-dimensions-or-more/

QUESTION 20
HOTSPOT

You are interviewing for a job as a Java developer. You need to evaluate the following code. Line numbers are
included for reference only.

For each of the following statements, select Yes if the statement is true. Otherwise, select No.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

Section: Topic 2, Work with data types, variables, and expressions


Explanation

Explanation/Reference:
References:
https://www.tutorialspoint.com/java/java_strings.htm

QUESTION 21
The question requires that you evaluate the underlined text to determine if it is correct.

You should use an int data type to store the numeric value 3,000,000,000 (3 billion) so that the least amount of
memory is used.

Review the underlined text. If it makes the statement correct, select “No change is needed.” If the statement is
incorrect, select the answer choice that makes the statement correct.

A. No change is needed.
B. a short
C. a byte
D. a long

Correct Answer: D
Section: Topic 2, Work with data types, variables, and expressions
Explanation

Explanation/Reference:

VCEConvert.com
QUESTION 22
You need to evaluate the following code segment:

What happens when the code segment is run?

A. iNum has a value of 0.


B. An exception is thrown.
C. iNum has a value of 2.
D. iNum has a value of 3.

Correct Answer: C
Section: Topic 2, Work with data types, variables, and expressions
Explanation

Explanation/Reference:

QUESTION 23
DRAG DROP

You are writing a Java method.

The program must meet the following requirements:

Accept a String parameter firstName


Display a welcome message that contains firstName
Ensure that the first letter of the name is capitalized, and the remaining letters are in lowercase

How should you complete the code? To answer, drag the appropriate code segment to the correct position.
Each code segment may be used once, more than once, or not at all. You may need to drag the split bar
between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

Correct Answer:

VCEConvert.com
Section: Topic 2, Work with data types, variables, and expressions
Explanation

Explanation/Reference:
References:
http://www.tutorialspoint.com/java/java_strings.htm

QUESTION 24
You work as a Java programmer.

You need to convert a numeric String to a primitive double value.

What code segment should you use?

A. Double.valueOf(numberString);
B. double.parseDouble(numberString);
C. String.parseDouble(numberString);
D. Double.parseDouble(numberString);

Correct Answer: B
Section: Topic 2, Work with data types, variables, and expressions
Explanation

Explanation/Reference:
References:
https://www.javacodeexamples.com/convert-string-to-primitive-example/140

QUESTION 25
DRAG DROP

Your instructor asks you to evaluate four arithmetic code segments.

What is the value of each code segment? To answer, drag the appropriate value from the column on the left to
its code segment on the right. Each data may be used once, more than once, or not at all. You may need to
drag the split bar between panes or scroll to view content.

Select and Place:

VCEConvert.com
Correct Answer:

Section: Topic 2, Work with data types, variables, and expressions


Explanation

Explanation/Reference:

QUESTION 26
HOTSPOT

You are writing a Java console program. The program accepts command line arguments.

You need to ensure that the main method parses and handles each command line argument.

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

VCEConvert.com
Section: Topic 2, Work with data types, variables, and expressions
Explanation

Explanation/Reference:

QUESTION 27
HOTSPOT

You work as an intern Java programmer at Adventure Works. Your team lead asks you to create a method.

The method must meet the following requirements:

Accept an int array


Check for duplicate values in the array
Stop the outer loop as soon as a duplicate value has been detected and return true
Return false if all values in the array are unique

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

VCEConvert.com
Section: Topic 3, Implement flow control
Explanation

Explanation/Reference:
References:
https://stackoverflow.com/questions/3951547/java-array-finding-duplicates

QUESTION 28
HOTSPOT

You are interviewing for a job as a Java developer. You need to demonstrate your understanding of switch
statements.

For each of the following code segments, select Yes if the code segment can be changed to a switch
statement with up to three case statements. Otherwise, select No.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

VCEConvert.com
Section: Topic 3, Implement flow control
Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

QUESTION 29
HOTSPOT

You are writing a Java method.

The method must meet the following requirements:

Accept a String array named entries


Iterate through entries
Stop the iteration and return false if any element has more than 10 characters
Otherwise, return true

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

VCEConvert.com
Section: Topic 3, Implement flow control
Explanation

Explanation/Reference:

QUESTION 30
DRAG DROP

Traders hires you to write a Java program to manage account openings.

To open a new account, a user must meet one the following requirements:

be over 65 years old and have a minimum annual income of 10,000


be at least 21 and have an annual income greater than 25,000

How should you complete the code? To answer, drag the appropriate operator to the correct position. Each
operator may be used once, more than once, or not at all. You may need to drag the split bar between panes
or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

VCEConvert.com
Correct Answer:

Section: Topic 3, Implement flow control


Explanation

Explanation/Reference:
References:
https://www.tutorialspoint.com/java/java_basic_operators.htm

QUESTION 31
HOTSPOT

You are writing a Java method named countdown.

The method must meet the following requirements:

Accept an int parameter named start


Display all numbers from start to zero in decrements of one

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

Section: Topic 3, Implement flow control


Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

QUESTION 32
HOTSPOT

You work as a Java programmer. A member of the team creates the following program. Line numbers are
included for reference only.

VCEConvert.com
The program is supposed to display a message to the console while it counts down from 60. The method does
not work as intended.

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

VCEConvert.com
Section: Topic 3, Implement flow control
Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

QUESTION 33
HOTSPOT

You are writing a Java console program.

The program must meet the following requirements:

Read a line of text entered by the user


Print each word in the text to the console on a separate line

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

VCEConvert.com
Hot Area:

Correct Answer:

Section: Topic 3, Implement flow control


Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

QUESTION 34
HOTSPOT

VCEConvert.com
You need to evaluate the following code. Line numbers are included for reference only.

Use the drop-down menus to select the answer choice that answers each question based on the information
presented in the code.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

Section: Topic 3, Implement flow control


Explanation

Explanation/Reference:

VCEConvert.com
QUESTION 35
HOTSPOT

You work for Woodgrove Bank as a Java programmer.

You need to evaluate the following class. Line numbers are included for reference only.

For each of the following statements, select Yes if the statement is true. Otherwise, select No.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

Section: Topic 4, Perform object-oriented programming


Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

QUESTION 36
HOTSPOT

You have the following code segment. Line numbers are included for reference only.

VCEConvert.com
The code does not compile.

For each of the following statements, select Yes if the action is required to resolve the compilation error.
Otherwise, select No.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

Section: Topic 4, Perform object-oriented programming


Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

QUESTION 37
DRAG DROP

You have a Java class named InsurancePolicy.

You need to define a constant data member named RATE. The data member must be accessible by any class
without instantiating the InsurancePolicy class.

VCEConvert.com
How should you complete the code? To answer, drag the appropriate code segment to the correct position.
Each code segment may be used once, more than once, or not at all. You may need to drag the split bar
between panes or scroll to view content.

NOTE: Each correct selection is worth one point.

Select and Place:

Correct Answer:

Section: Topic 4, Perform object-oriented programming


Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

QUESTION 38
HOTSPOT

You work on a team that develops e-commerce applications. A member of the team creates a class named DB
that connects to a database. The class contains a method named query.

You need to initiate the DB class and invoke the query method.

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

Section: Topic 4, Perform object-oriented programming


Explanation

Explanation/Reference:
References:
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#invoke-java.lang.Object-
java.lang.Object...-

QUESTION 39

VCEConvert.com
HOTSPOT

You are writing a Java class named SavingsAccount.

The class must meet the following requirements:

Inherit from an existing class named Account


Include a constructor that uses the base class constructor to initialize the starting balance
Include a substitute toString() method

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

VCEConvert.com
Section: Topic 4, Perform object-oriented programming
Explanation

Explanation/Reference:
References:
https://www.tutorialspoint.com/java/java_inheritance.htm

QUESTION 40
The question requires that you evaluate the underlined text to determine if it is correct.

You have the following class definition:

VCEConvert.com
The logError method can be invoked by code in all classes in the same package as the Logger class.

Review the underlined text. If it makes the statement correct, select “No change is needed.” If the statement is
incorrect, select the answer choice that makes the statement correct.

A. No change is needed
B. only by the Logger class
C. only by the Logger class and classes in the same package that inherit from it
D. by all classes in all packages

Correct Answer: C
Section: Topic 5, Compile and debug code
Explanation

Explanation/Reference:

QUESTION 41
HOTSPOT

You write the following Java program for Munson’s Pickles and Preserves Farm. Line numbers are included for
reference only.

You encounter error messages when you attempt to compile the program.

You need to ensure the program compiles successfully.

How should you complete the code? To answer, select the appropriate code segments in the answer area.

NOTE: Each correct selection is worth one point.

Hot Area:

VCEConvert.com
Correct Answer:

VCEConvert.com
Section: Topic 5, Compile and debug code
Explanation

Explanation/Reference:

QUESTION 42
HOTSPOT

You need to evaluate the output of the following code segment. Line numbers are included for reference only.

VCEConvert.com
For each of the following outputs, select Yes if the output is displayed. Otherwise, select No.

NOTE: Each correct selection is worth one point.

Hot Area:

Correct Answer:

VCEConvert.com
Section: Topic 5, Compile and debug code
Explanation

Explanation/Reference:
References:
https://www.w3schools.com/java/java_try_catch.asp

VCEConvert.com

You might also like