Question 2 - s3.amazonaws.com

advertisement
Question 1
1.
What will be the of the following program segment?
for(int count=13;count>=3;count-=3)
{
x=count%2;
System.out.print(x+" ");
}
1 11 2
1011
0121
1010
1 points
Question 2
1.
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count++<9)
{
System.out.println("Welcome to Java");
}
8
9
10
11
0
1 points
Question 3
1.
____________ is the code with natural language mixed with Java code
Java program
A Java statement
Pseudocode
A flowchart diagram
1 points
Question 4
1.
How many times will the following loop iterate?
for (int count=1; count!=100; count+=2)
System.out.println("Why did I do so badly on this test?");
5 times
50 times
78 times
infinitely
1 points
Question 5
1.
What is the output for the following program segment?
for (int count=1; count<10; count+=3)
System.out.print(count);
14710
369
147
1369
1 points
Question 6
1.
What will the output be from this loop?
for ( x = 2; x < 11; x -= -3)
{
number = x * 7
System.out.print(number+"; ");
}
2; 5; 8
2;
5;
8;
14; 35; 56;
14;
35;
56;
1 points
Question 7
1.
What is the value of the following expression?
true || true && false
True
False
1 points
Question 8
1.
Fill in the blanks to make the following code snippet produce the following output:
for(int y=0;y<=___;y++)
{
for(int x=0;___;x++)
System.out.print("*");
System.out.print("\n");
}
should produce:
*
**
***
3;x
2;x<=y
3;x>y
2;x>=y
1 points
Question 9
1.
Use the following code to answer this question. What is the value of count after the loop finishes.
int count = 1;
while (count <= 8)
{
System.out.println(count);
count += 2;
}
0
2
7
8
9
1 points
Question 10
1.
Suppose x=1, y=-1 and z=1. What is the printout of the following statement?
if (x>0)
if (y>0)
System.out.println("x>0 and y>0");
else if (z>0)
System.out.println(""x<0 and z>0");
x>0 and y>0
x<0 and z>0
x<0 and z<0
nothing prints
1 points
Question 11
1.
Analyze the following code:
boolean even=false;
if(even = true)
{
System.out.println("it is even!");
}
The program has a syntax error.
The program has a runtime error.
The program runs fine, but displays nothing.
The program runs fine and displays It is even!.
1 points
Question 12
1.
Which is true of the following boolean expression, given that x is a variable of type double?
3.0 == x * (3.0/x)
It will always evaluate to false
It may evaluate to false for some vlues of x.
It will ealuate to false only when x is zero.
It will evaluate to false only when x is very large or very close to zero.
It will always evaluate to true.
2 points
Question 13
1.
Given three integer variables, a, b, and c, with small non-negative values, which of the following code fragment
tests the condition that any two of the values are zeroes while the third one is positive? The variable ok should
be set to true if and only i the above conditions is true.
boolean ok =
a==0 && b==0 && c>0 ||
b==0 && c==0 && a>0 ||
c == 0 && a == 0 && b>0;
boolean ok = a + b + c > 0 && a* b + b*c + c*a ==0;
boolean ok = a > 0 || b>0 || c>0;
if (ok)
ok = a+b ==0 || b+c ==0 || c+a ==0;
none of the above meet the specified condition
2 points
Question 14
1.
What is y after the following switch statement is executed?
int y;
int x=3
switch (x+3)
{
case 6: y=0;
case 7: y=1;
case default: y++;
}
0
1
2
3
4
1 points
Question 15
1.
The expression 20 + 21 / 6 * 2 is equivalent to
20 + 3 * 2
41 / 6 * 2
20 + 21 / 12
41 / (6 * 2)
20 + (3.5 * 2)
2 points
Question 16
1.
Which of the following conditions must always be true after the following while loop finishes?
while (hours < hours0 || (hours == hours0 && mins < mins0))
{
mins =+ 5;
if (mins >= 0)
{
mins -= 60;
hours++
}
}
hours > hours0 && mins >- mins0
hours >= hours0 && mins >=mins0
hours < hours0 || (hours == hours0 && mnins < mins0)
hours >= hours0 && (hours != hours0 && mins >= mins0)
hours >= hours0 && (hours != hours0 || mins >=mins0)
2 points
Question 17
1.
Analyze the following code:
int i = 0;
for (i-0 ;i<10; i++);
System.out.println(i + 4);
The program has a syntax error because of the semicolon on the for loop line.
The program compiles despite the semicolon on the for loop line, and displays 4.
The program compiles despite the semicolon on the for loop line, and displays 14.
The for loop in this program is the same as for (i=0;i<10;i++){}System.out.println(i+4);
1 points
Question 18
1.
What is the printout of the following switch statement?
char ch-'a';
switch (ch)
{
case 'a':
case 'A':
system.out.print(ch);break;
case 'b':
case 'B':
system.out.print(ch);break;
case 'c':
case 'C':
system.out.print(ch);break;
case 'd':
case 'D':
system.out.print(ch);break;
}
abcd
a
aa
ab
abcd
1 points
Question 19
1.
Which of the following is the correct expression that evaluates to true if the number x is
between 1 and 100 or the number is negative?
1<x<100 && x<0
((x<100) && (x>1)) || (x<0)
((x<100) && (x>1)) && (x<0)
(1>x>100) || (x<0)
1 points
Question 20
1.
What is wrong with this loop? (select the best answer)
for (double x=1; x!=5; x+=0.1)
{
System.out.println(x);
}
Double cannot be used in loop control
0.1 cannot be accurately represented in binary therefore x may never equal 5
!= is not allowed in loop control
There is missing punctuation
1 points
Question 21
1.
Suppose x=10 and y=10. What is x after evaluating the expression (y>10) & (x++>10)
9
10
11
12
1 points
Question 22
1.
The loop below needs to be revised. Which is the BEST revision?
for(int x=2; inFile.hasNext(); x++)
{
System.out.println(x);
}
x++ needs to be changed to x=x+1
the data type of x needs to be changed to a double
no change is necessary
the for loop needs to be changed to a while loop in order to best display x
1 points
Question 23
1.
Suppose you want to read in an integer and print it if it's positive and even. Will the following code do the job?
(there are no errors, the program compiles)
int n=IO.readInt(); //this is correct for standard Java
if (n>0)
if (n % 2 == 0)
System.out.println(n);
else
System.out.println(n + " is not positive");
True
False
2 points
Question 24
1.
Alicia is five years older than her brother Ben. Three years from now Alicia's age will be twice Ben's age. How
old are Alicia and Ben now? Hal wrote the following program to solve this puzzle:
public class AliciaAndBen
{
public static void main (String[] args)
{
for (int a=1;a<=100;a++)
for (int b=1;b<=100;b++)
if (<condition>)
System.out.println("Alica is " + a + " and Ben is " +b);
}}
Which of the following expressions should replace <condition> in Hal's program so that it displays the correct
solution to the puzzle?
(a==b-5) && (a-3==2*(b-3))
a==b +5 && a+3==2*b+6
(a==(b+5)) && ((a+3)==(2*b+3))
a==(b-5) && (2*a-3)==(b-3)
None of the above will work.
2 points
Question 25
1.
Fill in the blank:
while ( _____)
a) end value b) boolean expression c) control value d) controlling expression
end value
boolean expression
control value
controlling expression
1 points
Question 26
1.
Which of the Boolean expressions below is incorrect?
(true) && (3=>4)
!(x>0) && (x>0)
(x>0)||(x<0)
(x != 0) || (x = 0)
(-10<x<0)
1 points
Question 27
1.
How many times is the condition in the while statement evaluated in the program segment below?
int count = 1;
while (count <= 8)
{
System.out.println(count);
count += 2;
}
0
5
7
8
9
1 points
Question 28
1.
Consider the code segment
if (n==1)
k++;
else if (n==4)
k+=4;
Suppose that the given segment is rewritten in the form
if (<condition>)
<assignment statement>:
Given that n and k are integers and that the rewritten code performs the same task as the original code, which of
the following could be used as
(1) <condition> and (2) < assignment statement>?
(1) n==1 && n==4
(2) k+=n
(1) n==1 && n==4
(2) k+=4
(1) n==1 || n==4
(2) k+=4
(1) n==1 || n==4
(2) k+=n
(1) n==1 || n==4
(2) k=n-k
2 points
Question 29
1.
The order of the precedence (from high to low) of the operators +,*, &&, ||, & is:
&&, ||, &, *, +
*, +, &&, ||, &
*, +, &, &&, ||
*, +, &, ||, &&
&, ||, &&, *, +
1 points
Question 30
1.
Which of the following will evaluate to true ONLY if boolean expressions A, B, and C are ALL false?
!A && !(B && !C)
!A || !B || !C
!(A || B || C)
!(A && B && C)
!A || !(B || !C)
2 points
Question 31
1.
Analyze the following code:
if (x < 100) & (x>10)
System.out.println("x is between 10 and 100");
The statement has syntax errors because (x<100 & (x>10) must be enclosed inside parentheses.
The statement has syntax errors because (x<100 & (x>10) must be enclosed inside parentheses and the
println(...) statement must be put inside a block.
The statement compiles fine.
The statement compiles fine but has a runtime error.
& should be replaced by && to avoid having a syntax error.
1 points
Question 32
1.
Analyze the following program fragment:
int x;
double d=1.5;
switch (d)
{
case 1.0: x=1;
case 1.5: x-2;
case 2.0: x-3;
}
The program has a syntax error because the required break statement is missing in the
switch statement.
The program has a syntax error because the required default case is missing in the
switch statemtn.
The switch control variable cannot be double
The program has no error.
1 points
Question 33
1.
Suppose income is 4001, what is the output of the following code:
if (income > 3000)
{
System.out.println("Income is greater than 3000");
}
else if (income > 4000)
{
System.outprintln("Income is greater than 4000");
}
no output
Income is greater than 3000
Income is greater than 3000 followed by Income is greater than 4000
Income is greater then 4000
Income is greater than 4000 followed by Income is greater than 3000
1 points
Question 34
1.
Which of the following represents the general syntax of a while loop?
while(expression) statement
while statement(expression)
(expression)while statement
(expression) statement while
Download