AP COMP SCI: Ch.3 (MC)

Ace your homework & exams now with Quizwiz!

int x = /* some integer value */ int y = /* some integer value */ boolean result = (x < y); result = ( (x > = y) && !result ); Which best describes the condition under which 'result' will be true after execution?

Only when (x >= y)

Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0?

if (x > 0) x++; else if (x < 0) x--;

int k = random # such that 1 <= k <= n for (int p = 2; p <= k; p++) for (int r = 1; r < k; r++) System.out.println("Hello"); what is the MINIMUM number of times "Hello" will be printed?

0

for (int outer = 0; outer < n; outer++) { for (inner = 0; inner <= outer; inner++) { System.out.print(outer + " "); } } If n has been declared with value of 4, what is printed as result?

0 1 2 3

A teacher put 3 bonus questions on a test and awarded 5 points to students who answered all 3 correctly and no points otherwise. which of the following will update the variable 'grade' based on student's performance on bonuses? 1. if (bonusOne && bonusTwo && bonusThree) grade += 5; 2. if (bonusOne || bonusTwo || bonusThree) grade += 5; 3. if (bonusOne) grade +=5; if (bonusTwo) grade +=5; if (bonusThree) grade +=5;

1 only

public String inRangeMessage (int value) { if (value < 0 || value > 100) return "Not in range"; else return "In range"; } Which of the following can replace the body of inRangeMessage? 1. if (value < 0) { if (value >100) return "Not in range"; else return "In range"; } else return "In range"; 2. if (value < 0) return "Not in range"; else if (value >100) return "Not in range"; else return "In range"; 3. if (value >= 0) return "In range"; else if (value <= 100) Return "In range"; else return "Not in range";

2 only

int a = 24; int b = 30; while (b != 0) { int r = a % b; a = b; b = r; } System.out.println(a); What is printed?

6

String str = "abcdef"; for (int rep = 0; rep < str.length() -1; rep++) { System.out.print(str.substring(rep, rep + 2)); } Which is printed as result?

abbccddeef

x and y are initialized boolean variables. (x && y) || !(x && y) the result of evaluating the expression above is:

always true

Consider the following code segment double a = 1.1; double b = 1.2; If ((a + b) * (a - b) != (a * a) - (b * b)) { System.out.println("Mathematical error!"); } Which of the following best describes why the phrase "Mathematical error!" would be printed? (Remember that mathematically (a + b) * (a - b) = a2 - b2)

Roundoff error makes the if condition true

Assume x,y,z are int variables, and that x and y have been initialized. Which statement best describes what this code segment does? if(y < 0) { x = -x; y = -y; } z =0; while (y>0) { z += x; y--; }

Sets z to be the product of x*y

The expression !(a && b) is logically equivalent to which of the following expressions?

!a || !b

Given 2 String variables, s1 and s2, to determine if they are the same length, which of the following conditions would you use?

( s1.length() == s2.length() )

int k = 0; while (k < 10) { System.out.print((k % 3) + " "); if ((k % 3) == 0 ) k = k+ 2; else k++; } What is printed as a result?

0 2 0 2 0 2 0

How many stars are output when this code segment is executed? for(int k = 0; k < 10; k++) For (int j = 0; j < 5; j++) System.out.println("*");

50

public int sol(int lim) { int s = 0; for (int outer = 1; outer <= lim; outer++) { for (int inner = outer; inner <=lim; inner++0 { s++; } } return s; } What value is returned as result of the call: sol(10)?

55

Given that s is a String, what does the following loop do? for (int j = s.length(); j > 0; j++) System.out.print(s.charAt(j-1));

It prints out s backwards

Given that s is a String, what does the following loop do? for(int j = s.length(); j >0; j--) System.out.print(s.charAt(j-1));

It prints s out backwards

Assume that a and b are variables of type int. The expression !(a < b) && !(a > b) Is equivalent to which of the following?

a == b

Assume that the following variable declarations have been made double d (hehe big boobs) = Math.random(); double r; Which of the following assigns a value to r from the uniform distribution over the range 0.5 <= r < 5.5?

r = d * 5.0 + 0.5;

int k = random # such that 1 <= k <= n for (int p = 2; p <= k; p++) for (int r = 1; r < k; r++) System.out.println("Hello"); What is the MAXIMUM number of times that "Hello" will be printed?

(n-1) ^2

What are the first and last #s printed by following code segment: int value = 15; while (value < 28) { System.out.println(value); value++; }

15, 27

Public void numberCheck (int maxNum) { Int typeA = 0; Int typeB = 0; Int typeC = 0; For (int k = 1; k <= maxNum; k++;) { If (k%2 == 0 && k%5 == 0) typeA++; If (k%2 == 0) typeB++; If (k%5 == 0) typeC++; } System.out.println(typeA + " " + typeB + " " + typeC); What is printed as a result of the call numberCheck(50)

D 5 25 10

Consider the following code segments int k = 1; while (k < 20) { if (k % 3 == 1) System.out.print(k + " "); k = k + 3; } II. for (int k = 1; k < 20; k++) { if (k % 3 == 1) System.out.print(k + " "); } III. for (int k = 1; k < 20; k = k +3) { if (k % 3 == 1) System.out.print(k + " "); } Which of the following code segments will produce the following output? 1 4 7 10 13 16 19

I. II. and III

Private int assignmentsCompleted; private double testAverage; public boolean isPassing() {implementation not shown} A student can pass a programming course if at least one of the following conditions is met The student has a test average that is greater than or equal to 90 The student has a test average that is greater than or equal to 75 and has at least 4 completed assignments Consider the following implementations of isPassing method If (testAverage >= 90) Return true; If (testAverage >= 75 && assignmentsCompleted >= 4) Return true; Return false; II. boolean pass = false; If (testAverage >= 90) Pass = true; If (testAverage >= 75 && assignmentsCompleted >= 4) Pass = true; Return pass; III. return (testAverage >= 90) || (testAverage >= 75 && assignmentsCompleted >= 4); Which of the implementations will correctly implement method isPassing?

I. II. and III.


Related study sets

Module 7 - Risk Reducing Strategies for Different Driving Environments

View Set

Chapter 16: Outcome Identification & Planning

View Set

Appendicular Division of Skeletal System

View Set

China & Japan (Honors World History 2)

View Set

Physiological psychology final set 6

View Set