Sunteți pe pagina 1din 12

Gaddis: Starting Out with Java: From Control Structures through Objects, 4/e

!"! #earson $ducation

Chapter 3 MULTIPLE CHOICE 1. The _________ statement is used to make simple decisions in Java. a. do/while b. for c. branch d. if

2. Which of the following expressions will determine whether x is less than or e ual to y! a. x > y b. x =< y c. x <= y d. x >= y

". What will be the value of x after the following code is executed! int x = 75; int y = 60; if (x > y) x = x - y; a. b. c. d. #$ 1$ %& 1"$

'. What will be the value of ans after the following code has been executed! int ans = 10; int x = 65; int y = 55; if (x >= y) ans = x + y; a. b. c. d. 1& 12& 1&& (o value) there is a s*ntax error

$. What will be the value of ans after the following code has been executed! int x = 0! y = 55! ans = 10; if ( x == y); ans "= #; a. b. 1& 1'$

Gaddis: Starting Out with Java: From Control Structures through Objects, 4/e

!"! #earson $ducation

c. d.

2& (o value) there is a s*ntax error

%. + block of code is enclosed in a set of a. braces $ % b. parentheses ( ) c. double uotes & & d. brackets ' (

#. + flag ma* have the values a. & or 1 b. ,1 or -1 c. tr)e or false d. of an* character

.. /f chr is a character variable) which of the following if statements is written correctl*! a. if (chr = &a&) b. if (chr == &a&) c. if (chr = *a*) d. if (chr == *a*)

0. /n Java) when a character is stored in memor*) it is actuall* stored as a1n2 _____. a. 3nicode number b. +45// character code c. 6758/5 character code d. 9orse code

1&. What will be the values of ans) x) and y after the following statements are executed! int ans = +5! x = 50! y =50; if ( x >= y) $ ans = x + 10; x -=y; % else $ ans = y + 10; y += x; % a. b. c. ans : %&) x : $&) y :1&& ans : %&) x :&) y :$& ans : '$) x : $&) y : &

Gaddis: Starting Out with Java: From Control Structures through Objects, 4/e

!"! #earson $ducation

d.

ans : '$) x : $&) y : $&

11. What will be the value of bon)s after the following code is executed! int bon)s! sales = 10000; if (sales < 5000) bon)s = #00; else if (sales < 7500) bon)s = 500; else if (sales < 10000) bon)s = 750; else if (sales < #0000) bon)s = 1000; else bon)s = 1#50; a. b. c. d. e. 2&& $&& #$& 1&&& 12$&

12. What would be the value of bon)s after the following statements are executed! int bon)s! sales = 1#50; if (sales > 1000) bon)s = 100; if (sales > 750) bon)s = 50; if (sales > 500) bon)s = #5; else bon)s = 0; a. b. c. d. 1&& $&& 2$ &

1". What would be the value of bon)s after the following statements are executed! int bon)s! sales = ,5000; char de-t = *.*; if (sales > 100000) if (de-t == */*) bon)s = #000; else bon)s = 1500;

Gaddis: Starting Out with Java: From Control Structures through Objects, 4/e

!"! #earson $ducation

else if (sales > 75000) if (de-t == */*) bon)s = 1#50; else bon)s = 1000; else bon)s = 0; a. b. c. d. 2&&& 1$&& 12$& 1&&&

1'. Which of the following is the correct boolean expression to test for; int x being a value between) but not including) $&& and %$&) or int y not e ual to 1&&&! a. ((x >= 500 00 x <= 650) 00 (y 1= 1000)) b. ((x > 500 234 x < 650) 5/ 1(y6e7)al(1000))) c. ((x > 500 00 x < 650) 88 (y 1= 1000)) d. ((x < 500 00 x > 650) 88 1(y == 1000))

1$. /f str1 and str# are both .trin9s) which of the following will correctl* test to determine whether str1 is less than str#! 112 (str1 < str#) 122 (str16e7)als(str#) < 0) 1"2 (str16co:-are;o(str#) < 0) a. b. c. d. 1) 2) and " will all work 2 " 2 and "

1%. To do a case insensitive compare which of the following could be used to test the e ualit* of two strings) str1 and str#! a. (str16e7)als<9nore=ase(str#)) b. (str16co:-are;o<9nore=ase(str#) == 0) c. <nl* a d. a and b

1#. What will be the value of -ay after the following statements are executed! int ho)rs = do)ble -ay! -ay = ho)rs >0 " >5; -ay/ate = 10600; <= >0 ? ho)rs " -ay/ate @ -ay/ate + (ho)rs - >0) " -ay/ate " 165;

Gaddis: Starting Out with Java: From Control Structures through Objects, 4/e

!"! #earson $ducation

a. b. c. d.

'&&.&& '$&.&& '%$.&& '#$.&&

1.. What would be the value of x after the following statements were executed! int x = 10; switch (x) $ case 10@ x += 15; case 1#@ x -= 5; breaA; defa)lt@ x "= +; % a. b. c. d. $ 2& 2$ "&

10. What will be printed when the following code is executed! do)ble x = >567,6#5 ; 4eci:alBor:at for:atter = new 4eci:alBor:at(&C!CCC!CC0600&); .yste:6o)t6-rintln(for:atter6for:at(x)); a. b. c. d. '$%#..2$0 &)&'$)%#..2% '$)%#..2% '$)%#.."

2&. Which of the following will format .12#. to displa* as 12..=! a. 00060D b. C0600D c. CCC6CCD d. CC060D

21. The expression tested b* an if statement must evaluate to a. & or 1 b. ,1 or -1 c. tr)e or false

Gaddis: Starting Out with Java: From Control Structures through Objects, 4/e

!"! #earson $ducation

d.

t or f

22. These operators are used to determine whether a specific relationship exists between two values. a. +rithmetic b. >elational c. 4*ntactical d. +ssignment

2". What is the value of x after the following code has been executed! int x = 75; int y = 0; if ( x 1= y) x += y; a. b. c. d. #$ 0& 1$ 1%$

2'. What is the value of ans after the following code has been executed! int x = >0; int y = >0; int ans = 0; if (x = y) ans = x + 10; a. b. c. d. $& .& "& (o value) this is a s*ntax error.

2$. What is the value of ans after the following code has been executed! int x = +5; int y = #0! ans = ,0; if (x < y); ans += y; a. b. c. d. .& 1&& "$ $$

Gaddis: Starting Out with Java: From Control Structures through Objects, 4/e

!"! #earson $ducation

2%. 6nclosing a group of statements inside a set of braces creates a a. block of statements b. boolean expression c. loop d. (othing) it is ?ust for readabilit*

2#. This is a boolean variable that signals when some condition exists in the program a. 4entinel b. 7lock c. @lag d. 5ase

2.. Which of the following correctl* tests the char variable chr to determine whether it is not e ual to the character 7! a. if (chr > *E*) b. if (chr < *E*) c. if (chr 1= *E*) d. if (chr 1= &E&)

20. /n an if/else statement) if the boolean expression is false) a. the first statement or block is executed b. the statement or block following the else is executed c. all statements or blocks are executed d. no statements or blocks are executed

"&. What will be the values of ans) x) and y after the following statements are executed! int ans = 0! x = 15! y =#5; if ( x >= y) $ ans = x + 10; x -=y; % else $ ans = y + 10; y += x; % a. b. c. d. ans : &) x : 1$) y : 2$ ans : 2$) x : -1&) y : 2$ ans : "$) x : 1$) y : '& ans : 2$) x : 1$) y : '&

Gaddis: Starting Out with Java: From Control Structures through Objects, 4/e

!"! #earson $ducation

"1. What would be the value of disco)nt/ate after the following statements are executed! do)ble disco)nt/ate = 060; int -)rchase = 100; if (-)rchase > 1000) disco)nt/ate = 605; else if (-)rchase > 750) disco)nt/ate = 60+; else if (-)rchase > 500) disco)nt/ate = 601; a. b. c. d. .&$ .&" .&1 &.&

"2. What would be the value of disco)nt/ate after the following statements are executed! do)ble disco)nt/ate = 060; int -)rchase = 1#50; if (-)rchase > 1000) disco)nt/ate = 605; if (-)rchase > 750) disco)nt/ate = 60+; if (-)rchase > 500) disco)nt/ate = 601; else disco)nt/ate = 0; a. b. c. d. .&$ .&" .&1 &

"". What would be the value of disco)nt/ate after the following statements are executed! do)ble disco)nt/ate = 060; int -)rchase = 1#50; char c)st = *3*; if (-)rchase > 1000) if (c)st == *F*) disco)nt/ate = 605; else disco)nt/ate = 60>; else if (-)rchase > 750) if (c)st == *F*) disco)nt/ate = 60>;

Gaddis: Starting Out with Java: From Control Structures through Objects, 4/e

!"! #earson $ducation

else disco)nt/ate = 60+; else disco)nt/ate = 0; a. b. c. d. .&$ .&' .&" &

"'. Which of the following is the correct boolean expression to test for; int x being a value less than or e ual to $&& or greater than %$&) and int y not e ual to 1&&&! a. ((x >= 500 00 x <650) 00 (y 1= 1000)) b. ((x <= 500 5/ x > 650) 234 1(y6e7)al(1000))) c. ((x >= 500 88 x < 650) 88 (y 1= 1000)) d. ((x <= 500 88 x > 650) 00 1(y == 1000))

"$. /f str1 and str# are both .trin9s) which of the following expressions will correctl* determine whether the* are e ual! 112 (str1 == str#) 122 str16e7)als(str#) 1"2 (str16co:-are;o(str#) == 0) a. 1) 2) and " will all work b. 1 and 2 c. 1 and " d. 2 and "

"%. What will be printed when the following code is executed! int y = 10; if ( y == 10) $ int x = +0; x += y; % .yste:6o)t6-rint(&x = &); .yste:6o)t6-rint(x); a. b. c. d. x = +0 x = >0 x = #0 x is unknown when the last statement is executed

"#. What will be the value of char9es after the following code is executed!

Gaddis: Starting Out with Java: From Control Structures through Objects, 4/e

!"! #earson $ducation

do)ble char9es! rate = 7600; int ti:e = 1,0; char9es = ti:e <= 11 ? rate " # @ ti:e G 6060 " rate; a. b. c. d. #.&& 1'.&& 21.&& 2..&&

".. What would be the value of disco)nt/ate after the following statements are executed! do)ble disco)nt/ate; char c)st;y-e = *E*; switch (c)st;y-e) $ case *2*@ disco)nt/ate = breaA; case *E*@ disco)nt/ate = case *=*@ disco)nt/ate = defa)lt@ disco)nt/ate = % a. b. c. d. .&. .&% .&' &.&

60,; 606; 60>; 060;

"0. What will be printed when the following code is executed! do)ble x = >567,6#5 ; 4eci:alBor:at for:atter = new 4eci:alBor:at(&C!CC060&); .yste:6o)t6-rintln(for:atter6for:at(x)); a. b. c. d. '$%#..2$0 '$)%#..2$0 '$)%#..2% '$)%#.."

'&. Which of the following will format 12.#. to displa* as &12..! a. 00060 b. C0600

Gaddis: Starting Out with Java: From Control Structures through Objects, 4/e

!"! #earson $ducation

c. d.

CCC6C CC060D

'1. What does the following code displa*! int d = ! e = 1#; .yste:6o)t6-rintf(&Dd DdHn&! d! e); a. Dd Dd 1# b. Dd c. d. D D1#

'2. What does the following code displa*! do)ble x = 1#6+7 ,1>6; .yste:6o)t6-rintf(&D6#fHn&! x); a. 1#+7 ,1>6 b. 1#+, c. D1#6+, d. 1#6+,

TRUE/FALSE 1. +n important st*le rule *ou should adopt for writing if statements is to write the conditionall* executed statement on the line after the if statement.

2. The if/else statement will execute one group of statements if its boolean expression is tr)e or another group if its boolean expression is false.

". 7ecause the 00 operator performs short-circuit evaluation) *our boolean expression will usuall* execute faster if the subexpression that is most likel* false is on the left of the 00 operator. '. + local variableAs scope alwa*s ends at the closing brace of the block of code in which it is declared. $. When testing for character values) the switch statement does not test for the case of the character.

%. +n important st*le rule *ou should follow when writing if statements is to line up the conditionall* executed statement with the if statement.

#. 3nicode is an international encoding s*stem that is extensive enough to represent +BB the characters of +BB the worldAs alphabets.

Gaddis: Starting Out with Java: From Control Structures through Objects, 4/e

!"! #earson $ducation

.. 7ecause the 88 operator performs short-circuit evaluation) *our boolean expression will generall* be evaluated faster if the subexpression that is most likel* to be tr)e is on the left. 0. When two .trin9s are compared using the co:-are;o method) the cases of the two strings are not considered.

1&. /n a switch statement) if two different values for the CaseExpression would result in the same code being executed) *ou must have two copies of the code) one after each CaseExpression.

S-ar putea să vă placă și