Midterm 1 W 02 Solt
Midterm 1 W 02 Solt
Which one of the following would be the best suitable type for a
variable that stores the number of dependents of the customer?
A. double
B. int
C. char
D. String
E. None of the above
A. double
B. int
C. char
D. String
E. None of the above
CSC142 – Midterm 1 2
A. would have more than two parameters and a void return type.
B. would have two parameters and a double return type.
C. would have less than two parameters and an int return type.
D. would have a void return type.
E. None of the above
A. "0"
B. "ab"+"cd"
C. '0'
D. A and B
E. A, B and C
After the above piece of code is executed, what are the colors of r1
and r2 (in this order)?
A. Color.blue
Color.red
B. Color.blue
Color.blue
C. Color.red
Color.red
D. Color.red
Color.blue
E. Can't tell. There is not enough information.
CSC142 – Midterm 1 4
9. What is the type and value of the following expression? (notice the
integer division)
A. int
-5
B. double
-4.5
C. int
-4
D. double
-5.0
E. None of the above
System.out.print("Hello,\nworld!");
A. Hello, \nworld!
B. Hello, world!
C. Hello,
world!
D. "Hello, \nworld!"
E. None of the above.
CSC142 – Midterm 1 5
11. In the class Car, you want to create an instance method isSpeeding to
check whether an object to type Car is speeding or not.
One of the instance fields of the Car class is a double variable speed
equal to the current speed of the Car object. The class also lists a
constant static double field SPEED_LIMIT, equal to the legal speed
limit for the driving conditions of the Car object.
What is printed?
A. a=3 s=Blue
B. a=5 s=Yellow
C. a=3 s=Yellow
D. a=5 s=Blue
E. The code doesn't compile. In Java, it is forbidden to use the same
name for a local variable in two different methods.
A. new Oval(80,80,40,40);
B. new Oval(80,80,20,20);
C. new Oval(60,60,40,40);
D. new Oval(120,120,20,20);
E. new Oval(100,100,20,20);
CSC142 – Midterm 1 7
A. int var;
B. int VAR;
C. int var1;
D. int var_1;
E. int 1_var;
15. Using De Morgan’s law, how would you rewrite the following
conditional statement
(i.e. is rewrite the statement using && instead of ||)
A. !(c!='n' || z+2<=5)
B. !(c=='n' || z+2>=5)
C. !(c!='n' || z+2<=5)
D. !(c=='n' || z+2>5)
E. !(c=='n' || z+2<5)
16. In a Java program, you read the following statement that compiles
and executes.
17. You are programming a game of dice. You need to generate a random integer
that can be 1, 2, 3, 4, 5, or 6.
Which of the following expression would you select?
A. Math.random()*6
B. ((int)Math.random())*6+1
C. (int)(Math.random()*6)+1
D. (int)(Math.random()+6)
E. (int)(Math.random()*6)
The method setValue assigns the value of i to the instance field value. What
could you write for the implementation of setValue?
A. value = i;
B. this.value = i;
C. value == i;
D. A and B
E. A, B and C.
CSC142 – Midterm 1 9
A. Equal
B. Not equal
C. Equal
Not equal
D. The program doesn't compile because == can't be used with
references.
E. The program doesn’t execute because i and j are not correctly
initialized.
20. Consider the following truth table for a logical operator "implies"
P Q P implies Q
T T T
T F F
F T T
F F T