Filters
Question type

Study Flashcards

Choose the loop that is equivalent to this loop. Int n = 1; Double x = 0; Double s; Do { S = 1.0 / (n * n) ; X = x + s; N++; } While (s > 0.01) ;


A) double x = 0;
Double s = 1;
For (int k = 1; s > 0.01; k++)
{
S = 1.0 / (k * k) ;
X = x + s;
}
B) double x = 0;
Double s = 1;
For (int k = 1; k < 100; k++)
{
S = 1.0 / (k * k) ;
X = x + s;
}
C) double x = 0;
Double s = 1;
Int k = 10;
While (s > 0.01)
{
S = 1.0 / (k * k) ;
X = x + s;
K++;
}
D) double x = 0;
Double s = 10;
Int k = 1;
While (s > 0.01)
{
S = 1.0 / (k * k) ;
X = x + s;
K++;

E) B) and D)
F) B) and C)

Correct Answer

verifed

verified

What will be the result of running the following code fragment? Int year = 0; Double rate = 5; Double principal = 10000; Double interest = 0; While (year < 10) { Interest = (principal * year * rate) / 100; System.out.println("Interest " + interest) ; }


A) The code fragment will display the interest calculated for nine years.
B) The code fragment will continue to display the calculated interest forever because the loop will never end.
C) The code fragment will not display the calculated interest and halt abruptly.
D) The code fragment will not display any output because it will not compile.

E) C) and D)
F) A) and B)

Correct Answer

verifed

verified

How many times does the following loop run? Int i = 0; Int j = 1; Do { System.out.println("" + i + ";" + j) ; I++; If (i % 3 == 0) { J--; } } While (j >= 1) ;


A) 1 time
B) 2 times
C) 3 times
D) 4 times

E) A) and C)
F) C) and D)

Correct Answer

verifed

verified

Which of the following activities can be simulated using a computer? I. Waiting time in a line at a restaurant II. Tossing a coin III. Shuffling cards for a card game


A) I only
B) II only
C) I and II only
D) I, II, and III

E) A) and B)
F) A) and C)

Correct Answer

verifed

verified

D

What is the output of the code snippet given below? String s = "abcdefghijkl"; Int i = 1; Do { If (i > 2) { System.out.print(s.substring (1, i) ) ; } I++; } While (i < 5) ;


A) abcd
B) bcde
C) bcbcd
D) cdef

E) B) and D)
F) None of the above

Correct Answer

verifed

verified

Which of the following is considered a loop with a bound that could be problematic?


A) for (i = 1; i != n; i++)
B) for (years = n; years < 0; years--)
C) for (i = 1; i <= n; i++)
D) for (int i = 1; i <= 10; i++)

E) A) and C)
F) B) and C)

Correct Answer

verifed

verified

What is the output of the following code snippet? Int i = 1; While (i != 9) { System.out.print(i + " ") ; I++; If (i == 9) { System.out.println("End") ; } }


A) 1 End
B) 1 End (infinite loop)
C) 1 2 3 4 5 6 7 8 End
D) 1 2 3 4 5 6 7 8 End (infinite loop)

E) None of the above
F) B) and D)

Correct Answer

verifed

verified

What will be printed by the statements below? Int a = 10; While (a > 5) { System.out.print (a + " ") ; A = a - 2; }


A) 10 9 8 7 6 5
B) 10 8 6 4
C) 10 8 6
D) 10 8

E) B) and D)
F) A) and B)

Correct Answer

verifed

verified

The process of hand-tracing code is valuable because


A) It is usually faster than just running the code.
B) It is the best way to design an algorithm.
C) You must already have a working program in order to do it.
D) It gives valuable insight that you do not get by running the code.

E) C) and D)
F) None of the above

Correct Answer

verifed

verified

What is the output of the code snippet given below? String s = "12345"; Int i = 1; Do { If (i > 1) { System.out.print) ; } } While (i < 5) ;


A) No output
B) No output (infinite loop)
C) 12345
D) 2345

E) All of the above
F) C) and D)

Correct Answer

verifed

verified

What does the following loop compute? Scanner in = new Scanner (System.in) ; Int sum = 0; Int count = 0; While (in.hasNextInt() ) { Int value = in.nextInt() ; If (value > 0) { Sum += value; Count++; } } Double result = (sum * 1.0) /count;


A) The average of all the integers in the input
B) The sum of all the positive integers in the input divided by the number of integers in the input
C) The average of all the positive integers in the input
D) The second smallest value in the input

E) B) and C)
F) All of the above

Correct Answer

verifed

verified

C

What will be printed by the statements below? Int val = 1; Int sum = 0; While (val < 5) { Sum = sum + val; Val++; } System.out.print (sum) ;


A) 4
B) 5
C) 10
D) 15

E) None of the above
F) A) and B)

Correct Answer

verifed

verified

Is the following code snippet legal? boolean b = false; Do { System.out.println("Do you think in Java?") ; } While (b !=B) ;


A) Yes, it is legal but does not print anything.
B) Yes, it is legal and prints "Do you think in Java?" once.
C) Yes, it is legal and prints "Do you think in Java?" twice.
D) No, it is not legal and gives a compilation error.

E) C) and D)
F) None of the above

Correct Answer

verifed

verified

What will be the output of the following code snippet? boolean token1 = true; While (token1) { For (int i = 0; i < 10; i++) { System.out.println("Hello") ; } Token1 = false; }


A) No output.
B) Hello will be displayed 10 times.
C) Hello will be displayed 9 times.
D) Hello will be displayed infinite times.

E) A) and B)
F) A) and C)

Correct Answer

verifed

verified

What is the output of the following code snippet? Int i = 1; While (i < 10) { System.out.print(i + " ") ; I = i + 2; If (i == 5) { I = 9; } }


A) 1 3 5
B) 1 3 9
C) 1 3 5 7 9
D) 1 3 5 9

E) C) and D)
F) A) and D)

Correct Answer

verifed

verified

What is the first and last value of i to be displayed by the following code snippet? Int n = 20; For (int i = 0; i <= n; i++) { For (int j = 0; j <= i; j++) { System.out.println("" + i) ; } }


A) 0 and 20
B) 1 and 20
C) 0 and 19
D) 1 and 19

E) None of the above
F) All of the above

Correct Answer

verifed

verified

What is the output of the following code snippet? double a = 2; Int n = 16; Double r = 1; Double b = a; Int i = n; While (i > 0) { If (i % 2 == 0) // n is even { B = b * b; I = i / 2; } Else { R = r * b; I--; } } System.out.println("r = " + r) ;


A) r = 16.0
B) r = 128.0
C) r = 4096.0
D) r = 65536.0

E) C) and D)
F) B) and C)

Correct Answer

verifed

verified

Which of the following conditions can be added to the code below so it will loop until the value of sum is greater than 100? Scanner in = new Scanner (System.in) ; Int sum = 0; Do { Sum += in.nextInt() ; } While (/* put condition here */)


A) sum != 0
B) sum <= 100
C) sum > 100
D) sum == 100

E) A) and D)
F) B) and C)

Correct Answer

verifed

verified

What is the output of the code fragment given below? Int i = 0; Int j = 0; While (i < 27) { I = i + 2; J++; } System.out.println("j=" + j) ;


A) j=27
B) j=12
C) j=13
D) j=14

E) A) and B)
F) C) and D)

Correct Answer

verifed

verified

What will be the range of the random numbers generated by the following code snippet? Random generator = new Random() ; Int r1 = generator.nextInt(50) + 1;


A) Between 1 and 49
B) Between 0 and 50
C) Between 0 and 49
D) Between 1 and 50

E) A) and C)
F) A) and B)

Correct Answer

verifed

verified

D

Showing 1 - 20 of 128

Related Exams

Show Answer