Java Quiz 2 – Expressions & Variables – Flashcards
Unlock all answers in this set
Unlock answersquestion
1. What is the output of the following code?
String username = "Tom"; int userAge = 22;
System.out.print(userAge);
System.out.print("nis ");
System.out.print(username);
System.out.println("'s age.");
a. 22 is Tom's age.
b. 22
is Tom's age.
c. 22 nis Tom's age.
d. 22 n
is Tom's age.
answer
ANS: B
question
2. What is the output of the following code?
String username = "Tom"; int userAge = 22;
System.out.println(userAge + "nis " + username + "'s age.");
a. 22 is Tom's age.
b. 22
is Tom's age.
c. 22 nis Tom's age.
d. 22 n
is Tom's age.
answer
ANS: B
question
3. What is the result of the following expression?
10 + 5 * 3 - 20
a. -5 b. 5 c. 25 d. -50
answer
ANS: B
question
4. What is the result of the following expression?
25 / 4 + 4 * 10 % 3
a. 19
b. 7.25
c. 3
d. 7
answer
ANS: D
question
5. What is the result of the following expression?
25.0 / 4 + 4 * 10 % 3
a. 19
b. 7.25
c. 3
d. 7
answer
ANS: B
question
6. What is the output of the following code?
int a=5, b=2, c=10;
c = c + a * b - 5;
a. -45 c. 25
b. 15 d. None of the above
answer
ANS: B
question
7. What is the value of z after the following statements are executed?
double x = 2.5, y = 4.0;
int z = (int) x + y;
a. 6 c. 6.5
b. 6.0 d. 7
answer
ANS: A
question
8. Which statement is equivalent to the following statement?
total = total + tax;
a. total = tax++; c. total += tax;
b. total = ++tax; d. total =+ tax;
answer
ANS: C
question
9. Which statement is equivalent to the following statement?
tax = tax + 1;
a. tax++; c. Both a and b
b. tax += 1; d. None of these
answer
ANS: C
question
10. What will be displayed as a result of executing the following code?
int x = 5, y = 20;
x += 32;
y /= 4;
System.out.println("x = " + x + ", y = " + y);
a. x = 32, y = 4
b. x = 9, y = 52
c. x = 37, y = 5
d. x = 160, y = 80
answer
ANS: C
question
11. What will be the value of z as a result of executing the following code?
int x = 5, y = 28;
float z;
z = (y / x);
a. 5.60
b. 5.6
c. 3.0
d. 5.0
answer
ANS: D
question
12. What is the value of i after the following statement is executed?
int i = '2' + '3';
a. 5 b. 101 c. will not compile d. none of these
answer
ANS: B
question
13. What is the value of i after the following statement is executed?
int j = 2 + 'a';
a. 2 b. 99 c. will not compile d. none of these
answer
ANS: B
question
14. What will be the output of the following code?
char ch = 'a';
System.out.print(++ch);
a. a b. b c. will not compile d. none of these
answer
ANS: B
question
15. What is the value of the cityState string after this statement is executed?
String cityState = "Milwaukee";
cityState = cityState + "," + "Wisconsin";
a. Milwaukee","Wisconsin c. MilwaukeeWisconsin
b. Milwaukee,Wisconsin d. Wisconsin
answer
ANS: B
question
16. What is the value of the cityState string after these statements are executed?
String cityState = "Milwaukee";
cityState = cityState + ",";
cityState = "Wisconsin";
a. Milwaukee, Wisconsin c. MilwaukeeWisconsin
b. Milwaukee,Wisconsin d. Wisconsin
answer
ANS: D
question
17. What do each of the following expressions evaluate to given the declaration:
int a = 4, b = 3, n = 4; String ans = "Y";
a. 1 <= 1
b. 1 < 1
c. false == (1 < 1)
d. true == (1 < 1)
e. (a+b) < 2*a
f. (n > 2) && (n < 6)
g. (n > 2) || (n == 6)
h. !(n < 6) (False)
i. (ans = = "Y") || (ans = = "y")
j. (ans = = "Y") && (ans = = "y")
k. (n = = 2) && (n= =7) || (ans = = "Y")
l. (n = = 2) && ((n= =7) || (ans = ="Y"))
answer
a. 1 <= 1 (True)
b. 1 < 1 (False)
c. false == (1 < 1) (True)
d. true == (1 < 1) (False)
e. (a+b) < 2*a (True)
f. (n > 2) && (n < 6) (True)
g. (n > 2) || (n == 6) (True)
h. !(n < 6) (False)
i. (ans = = "Y") || (ans = = "y") (True)
j. (ans = = "Y") && (ans = = "y") (False)
k. (n = = 2) && (n= =7) || (ans = = "Y") (True)
l. (n = = 2) && ((n= =7) || (ans = ="Y")) (False)