Test1 - Discover Dalton State

advertisement
Introduction to Programming I
CMPS 1301 (Hawkins)
Review Questions (Chapter 1 to Chapter 5)
True/False
1.
2.
3.
4.
5.
Java is an object-oriented programming language.
A variable may be assigned a value only once in a method.
A constant may be assigned a value only once in a method.
Each Java class must contain a main method.
You can identify a class that is an application because it contains a public static void
main() method.
Answer the following:
6. Find and correct errors in the following code:
public class Test
public void Main(String[ ] args) {
int j = i + 1;
int k = 5.5;
System.out.println("j is " + j + "and k is " + k)
}
}
7. Show the output of the following code:
public class Test {
public static void main(String[ ] args) {
int x1, x2, i, j, y, z;
x1 = 1;
x2 = 1;
y = 5 + x1- -;
z = 5 + ++x2;
i = 6 % 4;
j = 2 / 5;
System.out.println("x1 is " + x1);
System.out.println("x2 is " + x2);
System.out.println("i is " + i);
System.out.println("j is " + j);
System.out.println("y is " + y);
System.out.println("z is " + z);
}
}
8. Write a program that prints numbers from 1 to 50, but for numbers that are multiples of
5, print Fives, and for numbers that are divisible by both 2 and 3, print Sixes.
9. Provide examples of logic errors. How do programmers minimize logic errors in their
code?
10. What is wrong with the following statement? How could you correct it?
if(payRate < 5.65 && payRate > 60)
System.out.println("Error in pay rate");
11. Explain the use of the NOT operator.
12. What is the output of the following:
public class Test {
public static void main(String[ ] args) {
System.out.println(max(2, 1.2));
}
public static double max(int num1, double num2) {
System.out.println("max(int, double) is invoked");
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(double num1, int num2) {
System.out.println("max(double, int) is invoked");
if (num1 < num2)
return num1;
else
return num2;
}
}
Multiple Choice
13. If you attempt to add an int, a byte, a long, and a double, the result will be a
__________ value.
A. long
B. byte
C. double
D. int
14. Locating and repairing all syntax errors is part of the process of ____ a program.
A. interpreting
B. compiling
C. debugging
D. executing
15. Refers to the hiding of data and methods within an object.
A. Encapsulation
B. Inheritance
C. Passed
D. Instance
16. The expression (int)(76.0252175 * 100) / 100 evaluates to _________.
A. 76.03
B. 76.0252175
C. 76.02
D. 76
17. According to Java naming convention, which of the following names can be variables?
A. class
B. totalLength
C. TOTAL_LENGTH
D. FindArea
18. Which of the following is not a valid boolean expression.
A. (1 < x < 100)
B. (x = 1) || (x != 1)
C. (x =< 5) && (x>=5)
D. a, b, and c are all correct
19. What is y after the following switch statement?
int x = 0;
int y = 0;
switch (x + 1) {
case 0: y = 0;
case 1: y = 1;
default: y = -1
}
A.
B.
C.
D.
1
-1
0
None of the above
20. A variable that is declared inside a method is called ________ variable.
A. a static
B. an instance
C. a local
D. a global
21. What is the output of the following code: (Please indent the statement correctly first.
This will help you read the program.)
int x = 9;
int y = 8;
int z = 7;
if (x > 9)
if (y > 8)
System.out.println("x > 9 and y > 8");
else if (z >= 7)
System.out.println("x <= 9 and z >= 7");
else
System.out.println("x <= 9 and z < 7");
A.
B.
C.
D.
x > 9 and y > 8;
x <= 9 and z >= 7;
x <= 9 and z < 7;
None of the above.
22. Which of the following is not an advantage of using methods.
A. Using methods makes program run faster.
B. Using methods makes reusing code easier.
C. Using methods makes programs easier to read.
D. Using methods hides detailed implementation from the clients.
23. Which of the following method results in 8.0?
A. Math.round(8.5)
B. Math.floor(8.5)
C. Math.ceil(8.5)
D. (int)(8.5)
24. Which of the following code displays the area of a circle if the radius is positive.
A. if (radius <= 0) System.out.println(radius * radius * 3.14159);
B. if (radius != 0) System.out.println(radius * radius * 3.14159);
C. if (radius >= 0) System.out.println(radius * radius * 3.14159);
D. if (radius > 0) System.out.println(radius * radius * 3.14159);
Answers:
1.
2.
3.
4.
5.
6.
True
False
True
False
True
public class Test {
//need starting brace
public void main(String[] args) { //main method is lower case
int i = 0;
//need to initialize variable i before used
int j = i + 1;
int k =5;
//if int, use 5; otherwise use double for 5.5
System.out.println("j is " + j + "and k is " + k);
//need semicolon
}
}
7. x1 is 0
x2 is 2
i is 2
j is 0
y is 6
z is 7
8. public class Test {
public static void main(String[] args) {
for (int i = 1; i <= 50; i++)
if (i % 5 == 0)
System.out.println("Fives");
else if (i % 2 == 0 && i % 3 == 0)
System.out.println("Sixes");
}
}
9. Examples of logic errors include multiplying two values when you meant to add,
printing one copy of a report when you meant to print five, or forgetting to
produce a requested count of the number of times an event has occurred. Errors
of this type must be detected by carefully examining the program output. It is
the responsibility of the program author to test programs and find any logic
errors. Good programming practice stresses programming structure and
development that helps minimize errors.
10. As a single variable, no payRate value can ever be both below 5.85 and over 60
at the same time, so the print statement can never execute, no matter what
value the payRate has.
if(payRate < LOW || payRate > HIGH)
System.out.println("Error in pay rate");
11. You use the NOT operator, which is written as the exclamation point (!), to
negate the result of any Boolean expression. Any expression that evaluates as
true becomes false when preceded by the NOT operator, and accordingly, any
false expression preceded by the NOT operator becomes true.
12. max(int, double) is invoked
2.0
13. C
14. C
15. A
16. D
17. B
18. A
19. B (since no break, case1 and default are evaluated)
20. C
21. D (else if and else belongs to second if statement. Since the first if is false;
nothing is excuted)
22. D
23. B
24. D
Download