AP Computer Science - Unit 3 Test

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

if(rsvp) { System.out.print("attending"); } else { System.out.print("not attending"); }

Assume that the following variables have been properly declared and initialized. a boolean variable named rsvp an int variable named selection, where 1 represents "beef", 2 represents "chicken", 3 represents "pasta", and all other values represent "fish" a String variable named option1 a String variable named option2 (a) Write a code segment that prints "attending" if rsvp is true and prints "not attending" otherwise. Write the code segment below.

D) num = 50, min = 50, max = 50

Consider the following code segment, which is intended to set the Boolean variable inRange to true if the integer value num is greater than min value and less than max value. Otherwise inRange is set to false. Assume that inRange, num, min, and max have been properly declared and initialized. boolean isBigger; boolean isSmaller; boolean inRange; if (num < max) { isSmaller = true; } else { isSmaller = false; } if (num > min) { isBigger = true; } else { isBigger = false; } if (isBigger == isSmaller) { inRange = true; } else { inRange = false; } Which of the following values of num, min, and max can be used to show that the code does NOT work as intended? A) num = 20, min = 30, max = 50 B) num = 30, min = 20, max = 40 C) num = 40, min = 10, max = 40 D) num = 50, min = 50, max = 50 E) num = 60, min = 40, max = 50

D) def

Consider the following code segment, which uses properly declared and initialized int variables x and y and the String variable result. String result = ""; if (x < 5) { if (y > 0) { result += "a"; } else { result += "b"; } } else if (x > 10) { if (y < 0) { result += "c"; } else if (y < 10) { result += "d"; } result += "e"; } result += "f"; What is the value of result after the code segment is executed if x has the value 15 and y has the value 5 ? A) ad B) adf C) d D) def E) ef

B) true false

Consider the following code segment. String myString = new String("my string"); String yourString = new String(); yourString = "my string"; boolean dotEquals = myString.equals(yourString); boolean equalsEquals = (myString == yourString); System.out.print(dotEquals + " " + equalsEquals); What is printed as a result of executing the code segment? A) true true B) true false C) false true D) false false E) my string my string

B) true true false

Consider the following code segment. String str1 = new String("Happy"); String str2 = new String("Happy"); System.out.print(str1.equals(str2) + " "); System.out.print(str2.equals(str1) + " "); System.out.print(str1 == str2); What is printed as a result of executing the code segment? A) true true true B) true true false C) false true false D) false false true E) false false false

B) true

Consider the following code segment. boolean a = true; boolean b = false; System.out.print((a == !b) != false); What is printed as a result of executing this code segment? A) false B) true C) 0 D) 1 E) Nothing is printed because the expression (a == !b) != false is an invalid parameter to the System.out.print method.

C) true false false

Consider the following code segment. boolean a = true; boolean b = true; System.out.print((b || (!a || b)) + " "); System.out.print(((!b || !a) && a) + " "); System.out.println(!(a && b) && b); What output is produced when this code segment is executed? A) true true true B) true false true C) true false false D) false true false E) false false false

D) 115

Consider the following code segment. int m = 8; int n = 3; if (m + n > 10) { System.out.print(m + n); } if (m - n > 0) { System.out.print(m - n); } What, if anything, is printed as a result of executing the code segment? A) Nothing is printed. B) 5 C) 11 D) 115 E) 511

C) 68

Consider the following code segment. int quant = 20; int unitPrice = 4; int ship = 8; int total; if (quant > 10) { unitPrice = 3; } if (quant > 20) { ship = 0; } total = quant * unitPrice + ship; What is the value of total after this code segment has been executed? A) 20 B) 60 C) 68 D) 80 E) 88

A) true

Consider the following expression. (3 + 4 == 5) != (3 + 4 >= 5) What value, if any, does the expression evaluate to? A) true B) false C) 5 D) 7 E) No value; relational operators cannot be used on arithmetic expressions.

D) a < b != c < b

Consider the following variable declarations and initializations. int a = 2; int b = 6; int c = 3; Which of the following expressions evaluates to false ? A) a < b == c < b B) a > b == b < c C) a < b != b < c D) a < b != c < b E) a > b != b > c

B) (sweet && salty) || (sweet && sour)

In the following expression, sweet, salty, and sour are properly declared and initialized boolean variables. sweet && (salty || sour) Which of the following expressions is equivalent to the expression above? A) (sweet && salty) || sour B) (sweet && salty) || (sweet && sour) C) (sweet && salty) && (sweet && sour) D) (sweet || salty) && sour E) (sweet || salty) && (sweet || sour)

D) a = 1, b = 1, and c = 0

Consider the following code segment. int a = 1; int b = 0; int c = -1; if ((b + 1) == a) { b++; c += b; } if (c == a) { a--; b = 4; } What are the values of a, b, and c after this code segment has been executed? A) a = 0, b = 4, and c = 0 B) a = 0, b = 4, and c = 1 C) a = 1, b = 0, and c = -1 D) a = 1, b = 1, and c = 0 E) a = 1, b = 1, and c = 1

C) C

Consider the following code segment. String first = new String("duck"); String second = new String("duck"); String third = new String("goose"); if (first == second) { System.out.print("A"); } else if (second == third) { System.out.print("B"); } else if (first.equals(second)) { System.out.print("C"); } else if (second.equals(third)) { System.out.print("D"); } else { System.out.print("E"); } What is printed as a result of executing the code segment? A) A B) B C) C D) D E) E

B) (j != k) || (k <= m)

In the following expression, j, k, and m are properly declared and initialized int variables. !((j == k) && (k > m)) Which of the following is equivalent to the expression above? A) (j != k) || (k < m) B) (j != k) || (k <= m) C) (j == k) || (k < m) D) (j != k) && (k <= m) E) (j == k) && (k < m)

if(selection == 1) { System.out.print("beef"); } else if(selection == 2) { System.out.print("chicken"); } else if(selection == 3) { System.out.print("pasta"); } else { System.out.print("fish");

Assume that the following variables have been properly declared and initialized. a boolean variable named rsvp an int variable named selection, where 1 represents "beef", 2 represents "chicken", 3 represents "pasta", and all other values represent "fish" a String variable named option1 a String variable named option2 (b) Write a code segment that prints the food item associated with selection. For example, if selection is 3, the code segment should print "pasta". Write the code segment below. Your code segment should meet all specifications and conform to the example.

if(rsvp == true) { String option 1 = new String ("Thanks for attending. You will be served "); if(selection == 1) { System.out.print(option 1 += "beef."); else if(selection == 2) { System.out.print(option 1 += "chicken."); else if(selection == 3) { System.out.print(option 1 += "pasta."); else { System.out.print(option 1 += "fish."); } } else { String option1 = new String("Sorry you can't make it.") System.out.print(option 1); }

Assume that the following variables have been properly declared and initialized. a boolean variable named rsvp an int variable named selection, where 1 represents "beef", 2 represents "chicken", 3 represents "pasta", and all other values represent "fish" a String variable named option1 a String variable named option2 (c) Write a code segment that will store a dinner selection in option1 based on the values of rsvp and selection. The intended behavior of the code segment is described below. If rsvp is true, the code segment should store in option1 a string indicating the person's attendance and food choice. For example, if rsvp is true and selection is 1, the following string should be stored in option1. "Thanks for attending. You will be served beef." If rsvp is false, the following string should be stored in option1, regardless of the value of selection. "Sorry you can't make it." Write the code segment below. Your code segment should meet all specifications and conform to the examples.

if(option1.equals(option2)) { System.out.print(true); } else { System.out.print(false); }

Assume that the following variables have been properly declared and initialized. a boolean variable named rsvp an int variable named selection, where 1 represents "beef", 2 represents "chicken", 3 represents "pasta", and all other values represent "fish" a String variable named option1 a String variable named option2 (d) Write a code segment that will print true if the strings option1 and option2 contain the same values and will print false otherwise. Write the code segment below.

E) a = 3, b = 2, c = 1

Assume that the int variables a, b, c, and low have been properly declared and initialized. The code segment below is intended to print the sum of the greatest two of the three values but does not work in some cases. if (a > b && b > c) { low = c; } if (a > b && c > b) { low = b; } else { low = a; } System.out.println(a + b + c - low); For which of the following values of a, b, and c does the code segment NOT print the correct value? A) a = 1, b = 1, c = 2 B) a = 1, b = 2, c = 1 C) a = 1, b = 2, c = 3 D) a = 2, b = 2, c = 2 E) a = 3, b = 2, c = 1

E) if (a == b) { a++; b++; } else if (a < b) { a++; } else { b++; }

Consider the following code segment in which the int variables a and b have been properly declared and initialized. if (a < b) { a++; } else if (b < a) { b++; } else { a++; b++; } Which of the following code segments is equivalent to the code segment above? A) if (a <= b + 1) { a++; } else if (b <= a - 1) { b++; } else { a++; b++; } B) if (a + 1 <= b) { a++; } else if (b - 1 <= a) { b++; } else { a++; b++; } C) if (a - b < 0) { a++; } else if (b - a > 0) { b++; } else { a++; b++; } D) if (a != b) { a++; } else if (b != a) { b++; } else { a++; b++; } E) if (a == b) { a++; b++; } else if (a < b) { a++; } else { b++; }

E) Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pea" but not "pear".

Consider the following two code segments, which are both intended to determine the longest of the three strings "pea", "pear", and "pearl" that occur in String str. For example, if str has the value "the pear in the bowl", the code segments should both print "pear" and if str has the value "the pea and the pearl", the code segments should both print "pearl". Assume that str contains at least one instance of "pea". I. if (str.indexOf("pea") >= 0) { System.out.println("pea"); } else if (str.indexOf("pear") >= 0) { System.out.println("pear"); } else if (str.indexOf("pearl") >= 0) { System.out.println("pearl"); } II. if (str.indexOf("pearl") >= 0) { System.out.println("pearl"); } else if (str.indexOf("pear") >= 0) { System.out.println("pear"); } else if (str.indexOf("pea") >= 0) { System.out.println("pea"); } Which of the following best describes the output produced by code segment I and code segment II? A) Both code segment I and code segment II produce correct output for all values of str. B) Neither code segment I nor code segment II produce correct output for all values of str. C) Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pear" but not "pearl". D) Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pearl". E) Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pea" but not "pear".

A) Code segment I and code segment II produce the same output for all values of x and y.

Consider the following two code segments. Assume that variables x and y have been declared as int variables and have been assigned integer values. I. int result = 0; if (x > y) { result = x - y; System.out.print(result); } else if (x < y) { result = y - x; System.out.print(result); } else { System.out.print(result); } II. if (x < y) { System.out.print(y - x); } else { System.out.print(x - y); } Which of the following correctly compares the outputs of the two code segments? A) Code segment I and code segment II produce the same output for all values of x and y. B) Code segment I and code segment II produce the same output only when x is equal to y. C) Code segment I and code segment II produce the same output only when x is not equal to y. D) Code segment I and code segment II produce the same output only when x is less than y. E) Code segment I and code segment II do not produce the same output for any values of x and y.

A) I only

In the code segment below, the int variable temp represents a temperature in degrees Fahrenheit. The code segment is intended to print a string based on the value of temp. The following table shows the string that should be printed for different temperature ranges. Temperature Range and String to Print: 31 and below - "cold" 32-50 - "cool" 51-70 - "moderate" 71 and above - "warm" String weather; if (temp <= 31) { weather = "cold"; } else { weather = "cool"; } if (temp >= 51) { weather = "moderate"; } else { weather = "warm"; } System.out.print(weather); Which of the following test cases can be used to show that the code does NOT work as intended? I. temp == 30 II. temp == 51 III. temp == 60 A) I only B) II only C) I and II only D) II and III only E) I, II, and III

C) !(sunny || windy)

In the following expression, sunny and windy are properly declared and initialized boolean variables. !sunny && !windy Which of the following is equivalent to the expression above? A) sunny || windy B) !sunny || !windy C) !(sunny || windy) D) !(sunny && windy) E) !(sunny && !windy)

C) AD

The following code segment prints one or more characters based on the values of boolean variables b1 and b2. Assume that b1 and b2 have been properly declared and initialized. if (!b1 || b2) { System.out.print("A"); } else { System.out.print("B"); } if (!(b1 || b2)) { System.out.print("C"); } else { System.out.print("D"); } if (b1 && !b1) { System.out.print("E"); } If b1 and b2 are both initialized to true, what is printed when the code segment has finished executing? A) ABCD B) ABD C) AD D) BD E) BDE

if(x+len>10) { len = Math.max(x,len)-Math.min(x, len); } if(y+len>10 { len = Math.max(y,len)-Math.min(y, len); } System.out.print("side length = " + len + ", area = " + len*len); Draw.drawLine(x, y, x+len, y); Draw.drawLine(x+len, y, x+len, y-len); Draw.drawLine(x+len, y-len, x, y-len); Draw.drawLine(x, y-len, x, y);

This question involves the Draw class, which is used to draw line segments and squares on a 10-by-10 xy-coordinate grid. Consider the following description of the Draw class method drawLine. public static void drawLine(int x1, int y1, int x2, int y2) - Draws a line segment from (x1, y1) to (x2, y2) in a 10-by-10 xy-coordinate grid. All parameters are between 0 and 10, inclusive. The drawLine method is used to draw a line segment from the coordinate (x1, y1) to the coordinate (x2, y2). For example, the call Draw.drawLine(2, 5, 6, 4) will produce a figure. Assume that x, y, and len, have been properly declared and initialized, write the program code to draw a square below.


Set pelajaran terkait

Chapter 11 Special Reproductive Concerns: Infertility and Genetics

View Set

Exam 2 QUIZLETS: Ch 30, 32, 33, 36, 37, 48, 49, 57, & 58

View Set

CSC 415 Operating System Principles Unit 05

View Set

Graded Potentials, AP, and Synaptic Transmission

View Set