Download Test2 - Dalton State

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Programming I
CMPS 1301 (Hawkins)
Review Questions (Chapter 7 to Chapter 12)
True/False
1.
2.
3.
4.
5.
A variable may be defined in the initialization expression of the for loop
A char is just a String of length one.
Since arrays are objects in Java they inherit all the characteristics of java.lang.Object.
A RuntimeException need not be caught.
A try statement can have any number of associated catch statements, including none.
Answer the following:
6. 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.
7. Show the printout of the following code:
public class Test {
public static void main(String[] args) {
int i = 1;
while (i <= 4) {
int num = 1;
for (int j = 1; j <= i; j++) {
System.out.print(num + "bb");
num = num * 3;
}
System.out.println();
i++;
}
}
}
8. What is the difference between a while loop and a do-while loop?
9. Convert the following for loop into a while loop.
int sum = 0;
for (int i = 0; i < 100; i++) {
sum = sum + i;
}
10. Write a method that finds the number of occurrences of a specified character in the
string
11. The following complete program prints four lines when executed. Show the four lines
that are printed in the order in which they are printed:
public class ArrayTest {
public static void main(String[] args) {
int[ ] test = new int[2];
test[0] = test[1] = 5;
System.out.println(test[0] + "," + test[1]);
fiddle(test, test[1]);
System.out.println(test[0] + "," + test[1]);
}
static void fiddle(int[] test, int element) {
test[0] = 10;
test[1] = 11;
element = 12;
System.out.println(test[0] + "," + test[1] + "," + element);
test = new int[2];
test[0] = 20;
test[1] = 21;
System.out.println(test[0] + "," + test[1]);
}
}
12. What will be the value of loc after the following code is executed?
int loc;
String str = "The cow jumped over the moon.";
loc = str.indexOf("ov");
13. What is displayed on the console when the following program is run?
class Test {
public static void main(String[] args) {
try {
System.out.println("Welcome to Java");
int i = 0;
int y = 2 / i;
System.out.println("Welcome to HTML");
}
finally {
System.out.println("The finally clause is executed");
}
System.out.println("End of the block");
}
}
14. Write a method that takes in an array of numbers and displays the array in reverse
order
Multiple Choice
15.
This type of loop always executes at least once.
A. while
B. do-while
C. for
D. all of the above
16.
A.
B.
C.
D.
17.
What is each repetition of a loop known as?
cycle
revolution
orbit
iteration
What will be assigned to x if you execute the following code segment?
s = "java";
char x = s.charAt(4);
A.
B.
C.
D.
'a'
'v'
Nothing will be assigned to x, because the execution causes the runtime error
StringIndexOutofBoundsException.
none of the above
18. "AbA".compareToIgnoreCase("abC") returns ____
A. false
B. 0
C. -1
D. -2
19. This method converts any Object to a String:
A. convertString( )
B. toString( )
C. replace( )
D. substring( )
20. A ragged array is
A. A two-dimensional array when the rows are of different lengths
B. A one-dimensional array for which the number of elements is unknown
C. A two-dimensional array for which the number of rows is unknown
D. There is no such thing as a ragged array
21. Analyze the following program.
class Test {
public static void main(String[] args) {
try {
String s = "5.6";
Integer.parseInt(s);
int i = 0;
int y = 2 / i;
System.out.println("Welcome to Java");
} catch (Exception e) {
System.out.println(e);
}
}
}
A.
B.
C.
D.
An exception is raised due to Integer.parseInt(s)
An exception is raised due to 2 / i
The program has a compilation error
The program compiles and runs without exceptions
22. After you create an array variable, you still need to ____ memory space
A. create
B. organize
C. reserve
D. dump
23. How would you create an array named someNumbers that holds three rows and four
columns?
A. int[ ][ ] someNumbers = new int[4][3]
B. int[ ][ ] someNumbers = new int[3][4]
C. int[ ] someNumbers = new int[3][4]
D. double[ ][ ] someNumbers = new int[3][4]
24. When a program contains multiple ____ blocks, they are examined in sequence until a
match is found for the type of Exception that occurred.
A.
finally
B.
throw
C.
try
D.
catch
25. What will be printed after the following code segment is executed?
String str = "abc456";
int m = 0;
while ( m < 6 ) {
if (!Character.isLetter(str.charAt(m)))
System.out.print( Character.toUpperCase(str.charAt(m)));
m++;
}
A.
B.
C.
D.
ABC
456
abc456
ABC456
26. If, within one try statement you want to have catch clauses of the following types, in
which order should they appear in your program:
(1) Exception
(2) IllegalArgumentException
(3) RuntimeException
A. 1, 2, 3
B. 2, 1, 3
C. 2, 3, 1
D. 3, 2, 1
27. If a method does not handle a possible checked exception, what must the method have?
A. A try clause in its header
B. A catch clause in its header
C. A try/catch clause in its header
D. A throws clause in its header
Answers:
1.
2.
3.
4.
5.
6.
True
False
True
True
True
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");
}
}
7. 1bb
1bb3bb
1bb3bb9bb
1bb3bb9bb27bb
8. The difference between a do-while loop and a while loop is the order of
evaluating the continuation-condition and executing the loop body. In a while
loop, the continuation-condition is checked and then, if true, the loop body is
executed. In a do-while loop, the loop body is executed for the first time before
the continuation-condition is evaluated
9. int sum = 0;
int i = 0;
while (i < 100) {
sum = sum + i;
i++;
}
10. public static void Count(String text1, char letter) {
int count = 0;
for(int i = 0; i < text1.length(); i++) {
char ch = text1.charAt(i);
if(ch == letter)
count++;
}
System.out.println("The number of letters is " + count);
}
11. 5, 5
10, 11, 12
20, 21
10, 11
12. 15
13. Welcome to Java
The finally clause is executed
End of the block
14. public static void reverse(int [ ] numbers) {
for (i = numbers.length - 1; i >= 0; i--)
System.out.print("" + numbers[i] + " ");
System.out.println();
}
}
15. B
16. D
17. C
18. D
19. B
20. A
21. A (both A and B are cause exceptions, but A occurs first)
22. C
23. B
24. D
25. B
26. C
27. D