CS1 REVIEW for MIDTERM EXAM 10/15/01
1. Computer development and components of a computer system: see worksheet.
2. Construct a truth table for conjunction.
3. Any switch statement in Java can be rewritten as a nested if-else statement.
True or False?
4. Any nested if-else statement can be rewritten as a switch statement in Java .
True or False?
5. Height Description
4.5' - 5.0' very short
5.1' - 5.5' short
5.6' - 6.0' average
6.1' - 6.25' tall
6.3' - 7.0' very tall
The above decision table can be implemented as a:
a. switch statement b. switch statement and nested if-else statement
c. nested if-else statement d. neither switch or nested if-else statement
switch (ch){
case 'a':
case 'A': System.out.println("Excellent");
break;
case 'b':
case 'B': System.out.println("Very Good");
break;
case 'c':
case 'C': System.out.println("Good");
break;
case 'd':
case 'D': System.out.println("Minimally Satisfactory");
break;
default: System.out.println("Unsatisfactory");
}
6. The above statement is valid. True or False?
7. The above statement would be valid if the default line were omitted. True or False?
8. What would happen when the above statement is executed if the value stored in ch
is 'S'?
a. Unsatisfactory would be printed
b. an error would result
c. The user would be prompted to enter another letter from the list: a,b,c,d,f
d. Satisfactory would be printed
9. A switch statement may have a selector of any primitive type. True or False?
10. A switch statement may have an object of the String class as a selector.
True or False?
11. A sequence of characters enclosed within double quotes is:
a. illegal b. called a double c. called a string literal d. a string
Refer to the following declaration for questions 12 - 25
String myStr = "abcde";
What is the result of each of the following statements?
12. char ch1 = myStr.charAt(2); 13. char ch2 = myStr.charAt(0);
14. char ch3 = myStr.charAt(3); 15. int i = myStr.compareTo("ABC");
16. boolean ans = myStr.equals("abcdef"); 17. int l = myStr.length();
17. String str1 = myStr.substring( 1, 4); 18. String str1 = myStr.substring( 1 );
19. int i = myStr.indexOf( 'a' ); 20. int i = myStr.indexOf( 'a' ,3 );
21. int i = myStr.indexOf( 'cd' ); 22. int i = myStr.indexOf( 'z' );
23. When the statement String str = myStr.trim(); is executed
a. any leading or trailing spaces in myStr are eliminated.
b. str is the same as myStr.
c. myStr is changed.
d. str contains the same characters myStr except leading or trailing spaces.
24. String str = myStr.concat( "xyz");
Which of the following is NOT true?
a. Concat is not a valid method of the String class.
b. Result is the same as String str = myStr + "xyz");
c. "abcde+xyz" stored in str.
d. Concatenation of the two Strings occurs.
25. myStr.compareTo("abc") returns
a. a zero b. a negative value c. a positive value d. an error
26. Strings are objects. True or False?
27. Strings are primitive types. True or False?
28. When an expression's operands are of different primitive types, Java automatically
promotes the value of the less inclusive type to a value of the more inclusive type.
True or False?
29. In an assignment statement such as double d = 23;
The value of the less inclusive type is automatically promoted to the variable's type.
True or False.
Refer to the following code for Questions 30 - 32
double dVar = 3.14;
int iVar;
void methodX (int x)
{ ... }
30. This statement: iVar = dVar * 2; results in:
a. 6 being stored in iVar b. 6.28 stored in iVar
c. a syntax error d. automatic type casting
31. In this statement: methodX( (int) dVar );
a. (int) is not valid b. (int) is unnecessary
c. there are too many parentheses d. type casting is used
32. Consider the declaration statement int x, y, z = 5;
Which of the following statements are true?
a. The statement is not valid because initialization cannot take place in a
declaration statement.
b. The value 5 is stored in variables x, y and z.
c. The values 3,4,5 are stored in x, y, z, respectively.
d. The value 5 is stored in z.
33. The scope visibility modifier that allows access to a data member by a subclass is
a. public b. private c. protected d. please
34. The scope visibility modifier that allows access to a data member by any class is a. public b. private c. protected d. please
35. The scope visibility modifier that allows access to a data member within this class
only is
a. public b. private c. protected d. please
36. In choosing the scope of variables, we follow each of the following principle except:
a. Keep variables as local as possible.
b. Use constants as often as possible.
c. Avoid making a calculation more than once.
d. Use variables sparingly.
37. A method that is used by an object to store a value in another object's data field is
a(n):
a. accessor b. constructor c. modifier d. argument
38. A method that is called by prefixing it with a class name, not an object name is a(n):
a. accessor b. instance c. class d. argument
39. A method that can be applied to a class instance (i.e. an object) is a(n):
a. accessor b. instance c. class d. argument
40. Java promotes reusability of code by providing methods that can be used in your
programs without instantiating an object of the class in which they reside.
True or False?
41. The methods of the class Math are instance methods, so they can be used in your
code. True or False?
42. Complete this sentence to make it true:
If a statement has a ___________error, the statement cannot be translated and
your program cannot be executed.
43. Comments written in a program after the symbol // or between the symbols /* */
that enhance the readability of a program are called _______________________.
44. A fundamental property of object-oriented programming that allows the extension
of the definition of a class and the use of its methods in another class is called
_____________________.
45. A class that extends another class is called the
a. subclass b. superclass c. parent d. application
46. A class that is extended by another class is called the
a. subclass b. superclass c. child e. application
47. A class definition tells the compiler what an object is all about. True or False?
48. An object is an instance of a class. True or False?
49. An object tells the compiler what members a class contains. True or False?
50. Which of the following are the 3 parts of a class definition?
a. Header, data field declarations, method definitions.
b. Data field declarations, method definitions, block.
c. Header declaration, method definitions, scope.
d. Data field declarations, method definitions, executable instructions.
51. In a Java application the first method executed is
a. The first method in the support class.
b. The first method called in the client class.
c. The method with the header public static void main (String[] args).
d. Up to the programmer.
52. The _____________________statement names a package of classes defined
elsewhere that will be used by the class containing this statement at the top
of its file.
a. include b. extend c. applet d. import
53. The _____________________statement allows a package's classes to be
referenced directly by their names without using the package name.
a. include b. extend c. applet d. import
54. Object-oriented programming is a disciplined approach that supports the idea
that data and methods are intertwined. True or False?
55. Evaluate expressions containing arithmetic, relational and boolean operations such as:
int x = 6; int y = 8; a. (x > 5) && (y % x <= 1) b. (x <= 10) || (x / (y-7) > 3)
Write code as specified in these problems.
56. Write a program that inputs a number of seconds up to 18,000 and outputs the hours, minutes, and sesconds equivalent. (Hint: use integer division and modular division)57. Write an if statement that, given two real numbers, will negate the two values when both are negative or both are positive.
58. Write a program that asks for the last names of three people and displays their names in alphabetical order.
59. Write a program segment that allows the user to enter values and prints the number of positive values entered and the number of negative values entered. Use 0 as the sentinel.
60. What does the following fragment display?
int product = 1;
int counter = 2;
while (counter <= 5)
{product *= counter;
counter ++;
}
displayResult(product);