8. Which of the following pairs of variables are different from each other?
a. Total and total
b. case and Case
c. codeTwo and code2
d. oneMore and one_More
e. all of the above
9. What will be printed by the following statement?
System.out.println(“Use a \”\\\””);
a. Use a \”\\\”
b. “Use a “\””
c. Use a “\”
d. Use a \”\””
e. Use a “\\”
10. Which keyword is used to declare a constant?
a. int
b. double
c. MAX
d. constant
e. final
11. The expression “number” + 6 + 4 * 5 produces which of the following string
literals?
a. “number645”
b. “number105”
c. “number50”
d. “number620”
e. “number26”
12. Which of the following is a character literal?
a. b
b. ‘b’
c. “b”
d. 2
e. 2.0
13. What is the result of the operation 30 % 4?
a. 2
b. 3
c. 4
d. 7
e. 7.5
14. The expression 1 / 4 is equal to which of the following?
a. 1.0 / 4.0
b. (double) 2 / 8
c. 0.25
d. (int) 1.0 / 4.0
e. 1 / (int) 4.0
15. Which of the following instantiates a String object?
a. String word;
b. word = new String(“the”);
c. word.length();
d. word = name;
e. String word = name;
16. Assuming g is an instance of the Random class, which statement will generate
a random number between 10 and 100 inclusive?
a. num = g.nextInt(101);
b. num = 10 + g.nextInt(101);
c. num = 10 + g.nextInt(91);
d. num = 10 + g.nextInt(90);
e. num = g.nextInt(110) – 10;
17. Which statement would we use to create an object from a class called Thing?
a. Thing something;
b. Thing something = Thing ();
c. Thing something = new Thing;
d. Thing something = new Thing ();
e. new Thing = something;
18. Suppose we have a variable something that is a reference to a Thing object.
How would we call the method doIt on our Thing object?
a. doIt();
b. something.doIt();
c. doIt(something);
d. something/doIt;
e. something(doIt);
19. Which of the following statements increase the value of x by 1?
i. x++;
ii. x = x +1;
iii. x += 1;
a. i only
b. ii only
c. i and iii
d. ii and iii
e. i, ii, and iii
20. What will be printed by the following code segment?
boolean flag = true;
int x = -1;
if (flag && (x > 0))
System.out.println(“yes”);
else if (x == 0)
System.out.println(“maybe”);
else if (!flag)
System.out.println(“sometimes”);
else
System.out.println(“no”);
a. yes
b. maybe
c. sometimes
d. no
e. There will be an error because you can’t mix integers and Booleans in
the same expression.
21. The expression !f || g is the same as which of the following?
a. f || !g
b. !(f || g)
c. !(f && g)
d. !(!f && !g)
e. !(f && !g)
22. In the following code, what value should go in the blank so that there are
exactly six lines of output?
for (int x = 0; x < _________; x = x +2)
System.out.println(“-“);
a. 5
b. 6
c. 10
d. 11
e. 13
23. What will be the largest value printed by the following code?
for (int x = 5; x > 0; x--)
for(int y = 0; y < 8; y ++)
System.out.println(x * y);
a. 5
b. 8
c. 35
d. 40
e. 64
24. Assume x is an integer and has been initialized to some value. Consider the
code:
for (int a = 1; a < 20; a ++)
if (x < 0)
x = a;
Which statement will have the same effect on the value of x?
a. if ( x < 0 ) x = 1;
b. if (x < 20) x = 19;
c. if (x < 0) x = 19;
d. if (x < 20) x = 20;
e. x = 1;
25. Assume num and max are integer variables. Consider the code:
while (num < max)
num++;
Which values of num and max will cause the body of the loop to be executed
exactly once?
a. num = 1, max = 1;
b. num = 1, max = 2;
c. num = 2, max = 2;
d. num = 2, max = 1;
e. num = 1, max = 3;
26. Which for loop is equivalent to this while loop?
int y = 5;
while (y >= 0)
{
System.out.println(y);
y--;
}
a. for (int y = 0; y < 5; y ++)
System.out.println(y);
b. for (int y = 5; y > 0; y --)
System.out.println(y);
c. for (int y = 5; y >= 0; y --)
System.out.println(y);
d. for (int y = 0; y > 5; y ++)
System.out.println(y);
e. for (int y = 0; y > 5; y --)
System.out.println (y);
27. Which expression tests to make sure the grade is between 0 and 100
inclusive?
a. (grade <= 100) || (grade <= 0)
b. (grade <= 100) || (grade >= 0)
c. (grade <101) || (grade > -1)
d. (grade <= 100) && (grade >= 0)
e. (grade >= 100) && (grade <= 0)
28. Which values of x, y, a, or b will cause the if statement to be short-circuited?
if ((x > y) && (a || b))
statement;
a. x = 1, y = 1
b. x = 5, y = 1
c. x = 2, y = 1, a = true, b = false
d. a = false, b = true
e. a = false, b = false
Questions 29-32 refer to the following class.
public Point ()
{
myX = 0;
myY = 0;
}
29. Which of the following statements creates a point with coordinates (0,0)?
i. p = new Point ();
ii. p = Point (0, 0);
iii. p = (0,0);
a. i only
b. ii only
c. iii only
d. i and ii only
e. ii and iii only
30. Which of the following statements is true regarding the Point class?
a. The class won’t compile because there are two methods name Point.
b. Variable myX and myY can be changed from outside of the class.
c. Point objects are immutable.
d. It’s impossible to create Point objects with coordinates other than (0,
0).
e. Giving myX and myY private visibility was a poor design decision.
31. Suppose we want to add to the Point class a method with the following
signature:
//Sets the x coordinate of the point to the given value
public void setX (int x)
What is printed?
a. 5 6
b. 5 5
c. 6 6
d. p q
e. There is a compile error because the return value of doSomething is
not stored in a variable.