Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
74 views4 pages

Unit 1 Test

The document contains a test on primitive types in programming, featuring multiple-choice questions and free response sections. It assesses knowledge on code segments, data types, and error identification in programming syntax. The test includes calculations and logical reasoning to determine correct outputs and fixes for given code snippets.

Uploaded by

coconutboy303
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)
74 views4 pages

Unit 1 Test

The document contains a test on primitive types in programming, featuring multiple-choice questions and free response sections. It assesses knowledge on code segments, data types, and error identification in programming syntax. The test includes calculations and logical reasoning to determine correct outputs and fixes for given code snippets.

Uploaded by

coconutboy303
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/ 4

Unit 1: Primitive Types​ ​ ​ ​ ​ ​ ​ Name: Adhit Natla

Test​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

Multiple Choice

___A__ 1) Which of the following code segments will correctly print a value (result) that is 1 more than
twice the value of n?

I. int result = 2 * n; II. int result = n + 1; III. int result = (n + 1) * 2;


result = result + 1; result = result * 2; System.out.println(result);
System.out.println(result); System.out.println(result);

(A) I​ ​ (B) I and II​ ​ (C) II and III​ ​ (D) I and III​ ​ (E) I, II, and III

__E___ 2) Consider the following code segment.

int x = 6;
int y = 3;
int z = 2;
System.out.println(x + y / z * 2);

What is printed as a result of executing this code?

(A) 9​ ​ (B) 10​ ​ (C) 7​ ​ (D) 16​ ​ (E) None of these

__E___ 3) Consider the following code segment.

double number = 11/4;


System.out.print(number);
System.out.print(“ “);
System.out.print((int) number);

What is printed as a result of executing the code segment?

(A) 2.75 2 ​ (B) 2.75 3​ (C) 2.0 2.0 ​ (D) 2.75 2.0 ​ (E) 2.0 2

___A__ 4) Consider the following code segment.

double a = (int) (6.5 – 3.5);


double f = (int) 6.5 – 3.5;
System.out.print(a – f);

What is printed as a result of executing the code segment?

(A) 0.5​​ (B) 0.0​​ (C) 1​ ​ (D) -1​ ​ (E) -0.5



__B___ 5) Consider the following code segment.

int g = 8;
int h = 4;
int i = 2;
g *= 3;
h += g;
h /= i;
System.out.print(h);

What is printed when the code segment is executed?

(A) 6 ​ ​ (B) 14​ ​ (C) 9​ ​ (D) 10​ ​ (E) None of these

__D___ 6) Which of the following expressions evaluates to 7?

I. 9 + 10 % 12
II. (9 + 10) % 12
III. 9 – 2 % 12

(A) I and II​ ​ (B) II​ ​ (C) II and III​ ​ (D) I and III​ ​ (E) I, II, and III

__E___ 7) Which of the following statements stores the value 5 in x?

(A) int x = 5 / 7;
(B) int x = 7 / 5;
(C) int x = 7 / 4;
(D) int x = 7 % 5;
(E) int x = 5 % 7;

__A___ 8) Which of the following data types would be most appropriate to use when recording whether a
switch is in the “on” or “off” position?

(A) boolean
(B) int
(C) double
(D) String
(E) char
__A___ 9) Consider the following code segment.

int m = 4 + 5 * 2;
int j = 1 + 5 / 4;
int k = 7 % 2 + 3;
double s = m + j + k;

What is the value of s after the code segment is executed?

(A) 20.0​ (B) 24.0​ (C) 20.25​ (D) 23.0​ (E) None of these

__D___ 10) Consider the following code segment, where i and count are properly declared and initialized int
variables.

i++;
i++;
count++;
i--;
count++;
i--;

Which of the following best describes the behavior of the code segment?

(A) The code segment leaves both i and count unchanged.


(B) The code segment increase both i and count by 2.
(C) The code segment increase i by 4 and count by 2.
(D) The code segment leaves i unchanged and increases count by 2.
(E) The code segment increase i by 2 and leaves count unchanged.

Free Response

11) For the following expressions, add parentheses so you obtain the intended value. Hint: You only need to add
a single set of parentheses around the expression to get it to work.

int a = 1, b = 2, c = 3, d = 4;

a) a + (b * c + d / b) % 3​​ ​ ​ b)( a + b * c )+ d / b % 3

Value: 3​ ​ ​ ​ ​ ​ ​ Value: 9



12) There are 4 errors with the following program. Point them out and identify how to fix them.

private should be public class Test1 {

public static void main(String[] args) {

int tripMiles = 300 Missing semi colon, add it

DOUBLE should be double price = 2.50;

int milesPerGallon = 36;

double numberOfGallons = tripMiles * 1.0 / milesPerGallon;

double totalCost = numberofgallons (wrong var name)* price;

System.out.println(totalCost);

private class Test1 should be public class Test1

int tripMiles = 300 missing semicolon (;)

DOUBLE price = 2.50; should be double price = 2.50;

numberofgallons should be numberOfGallons (case-sensitive variable name)​



13) Write a line of code to calculate the number of seconds in 3 days and store it in an appropriately named
integer variable. Remember that there are 60 seconds in a minute and 60 minutes in an hour and 24 hours in a
day.

int secondsInThreeDays = 3 * 24 * 60 * 60;

You might also like