Download Final Exam

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
Final Exam
CompSci 119
Old Practice Final
Student Name:
1. [2 pts] I found the CS-119 text book, “Java: How to Program” to be:
a. Helpful for understanding the lectures and I’ll retain it as a Java reference
b. Helpful but I’m not keeping it
c. Not helpful
d. I don’t or no longer have the book
2. [2 pts] The preferred micro-blogging site for interacting with this course would be:
a. Facebook
b. Blackboard
c. Google+
d. None of these; I dislike/distrust/avoid social networking, even for a class
3. [2 pts] I prefer to do most of my CS-119 homework using:
a. Eclipse on my own computer
b. Eclipse on a BSU computer
c. The vi/emacs/etc editor and JDK on my own computer
d. The vi/emacs/etc editor and JDK on a BSU computer
4. [2 pts] The return statement:
a. Returns control to the first statement in the enclosing method
b. May be used to return a value to the caller of the enclosing method
c. All of the above
d. None of the above
6. [2 pts] Every location in a computer’s Random Access Memory (RAM):
a. Has an address
b. May contain data
c. May contain machine language instructions
d. All of the above
7.
[2 pts] Reference types include:
a. int and boolean
b. float and double
c. char
d. classes and arrays
8. [2 pts] A primitive type:
a. Persists even after the computer’s power is shutoff
b. Must be the name of a previously defined class
c. Can be defined by the programmer to extend the Java language
d. None of the above
9. [2 pts] Consider statement: int n = 3;
a. Defines a new primitive type called int and initializes it with the value 3
b. Defines a variable called n and initializes it with the address of the value 3
c. Tests to see if the value of n is equal to 3
d. Defines a variable called n of type int and initializes it with the value 3
10. [2 pts] Consider statement: Scanner stdin;
a. Defines a variable called stdin of type Scanner
b. Creates (news up) an object called stdin of type Scanner
c. Defines a new reference type called stdin of type Scanner
d. All of the above
5. [2 pts] public static void main(String[] args)
a. Defines a class called HelloWorld
b. Is the method invoked by the Java runtime environment when a program starts
c. Should not include the keyword public
d. Requires the main method be imported before use in this program
11. [2 pts] Consider statement: Scanner stdin = new Scanner(System.in);
a. Defines a class called stdin
b. Invokes a Scanner!constructor
c. Deletes the old Scanner!class!and!builds!a!new!one
d. All of the above
12. [2 pts] A Java variable:
a. Always requires at least 8 bytes of memory
b. Has an assigned storage location (address) in memory
c. Is an instance variable of some class
d. All of the above
13. [2 pts] A Java object:
a. Is an instance (implementation) of a primitive type
b. Is an instance (implementation) of a class
c. Is a defect in a Java program
d. None of the above
14. [2 pts] A Java code block:
a. Occurs when a Java programmer can’t determine how to get started
b. Occurs when a Java programmer can’t determine how to proceed
c. Can be a sequence of Java statements within braces
d. Abruptly terminates a Java program with an error message
15. [2 pts] A variable of the Java double type:
a. Requires 8 bytes of memory
b. May contain only integer values
c. Contains the address of the number’s value
d. All of the above
16. [2 pts] The Java expression: int n = (5 + 15) / 2 + 5 * 2;
a. Evaluates to 20
b. Evaluates to 1
c. Evaluates to 30
d. Generates an error
17. [2!pts]!The!Java!expression:!!double n = 3.0 + 7.0 / 2.0;
a. Evaluates to 10.0
b. Evaluates to 5.0
c. Evaluates to 6.0
d. Evaluates to 6.5
18. [2!pts]!The!Java!expression:!!boolean q = true || false;
a. Evaluates to false
b. Evaluates to true
c. Generates an error
d. None of the above
19. [2!pts]!The!Java!expression:!!boolean f = ((3<5) && (3>5));
a. Evaluates to false
b. Evaluates to true
c. Generates an error
d. None of the above
20. [2 pts] Memory for storing variables:
a. Storage for an instance variable (field) is allocated when a method is called
b. Storage for a static variable (static field) is allocated when a method is called
c. Storage for a parameter is allocated when a class is loaded into memory
d. Storage for local variables and parameters is allocated when a method is called
21. [2 pts] The following is a legal identifier in Java:
a. class
b. sponge bob
c. sponge-bob
d. sponge_bob
22. [2 pts] Arguments:
a. Are a type of instance variable
b. Must be primitive types
c. Are the actual values passed to a method by its caller
d. None of the above
23. [2 pts] An instance variable (field):
a. Is not accessible by any method
b. Is accessible by every method in its enclosing class
c. Must have a primitive type
d. Cannot be initialized
24. [2 pts] A local variable:
a. Is not accessible by any method
b. Is accessible by every method in its enclosing class
c. Is accessible by the method which defines it
d. Must have a primitive type
25. [2 pts] The memory for a local variable:
a. Is allocated when the method defining it is called
b. Is recycled (discarded) when the method defining it finishes (returns)
c. All of the above
d. None of the above
31. [2 pts] A class:
a. Must not define a constructor
b. May define zero, one or more constructors
c. May define at most one method
d. None of the above
32. [2 pts] The name of a constructor:
a. May be any Java identifier
b. Must match the name of the first method in the class
c. Need not be provided
d. Must match the name of the class
!
33. [2 pts] Sentinel-controlled repetition is commonly used:
a. Because it’s faster for the computer to execute
b. When the number of required iterations is known in advance
c. When the number of required iterations is unknown in advance
d. Only when counting up, not when counting down
34. [2 pts] The code block of this loop will always be executed at least once:
a. while
b. for
c. do while
d. switch/case
35. [2 pts] This loop is popular for counter-controlled repetition
a. switch/case
b. for
c. enumeration types
d. while
26. [2 pts] The memory for an instance variable is allocated:
a. When the enclosing object is destroyed
b. When the enclosing object is “newed up”
c. Is never allocated as it’s not required
d. None of the above
27. [2 pts] An instance variable (field):
a. Usually should be declared as public so anything can access it
b. Usually should be declared as private to conceal its current implementation
c. Usually should be declared as static to ensure it won’t disappear
d. Usually should be declared as void to ensure it never returns anything
28. [2 pts] Parameters:
a. Are a type of instance variable
b. Must be primitive types
c. Are never allocated memory
d. Refer to the list of variables in a method’s declaration
29. [2 pts] A static variable:
a. Is defined with the static keyword
b. Has a single instance shared amongst all objects of the defining class
c. Has the same value in every instance of the defining class
d. All of the above
30. [2 pts] A static method:
a. May be invoked without newing up an object of the class
b. Is invoked by providing the class name, then a “.” and then the method name
c. Is associated with a class rather than an object
d. All of the above
Consider the following Java program segment:
Scanner stdin = new Scanner(System.in);
int month = stdin.nextInt();
switch (month) {
case 1:
case 2: System.out.println(“Winter”);
break;
case 4:
case 5: System.out.println(“Spring”);
break;
case 7:
case 8: System.out.println(“Summer”);
break;
case 10:
case 11: System.out.println(“Fall”);
default: System.out.println(“Ignored);
break;
}
36. [2 pts] If the program reads 1 from stdin, it will print words:
a. Winter
b. Spring Summer Fall
c. Spring Summer Fall Ignored
d. Ignored
37. [2 pts] If the program reads 3 from stdin, it will print words:
a. Spring Summer Fall
b. Spring Summer Fall Ignored
c. Spring
d. Ignored
38. [2 pts] If the program reads 10 from stdin, it will print:
a. Fall Ignored
b. Winter Spring Summer Fall
c. Fall
d. Ignored
39. [2 pts] If the program reads 0 from stdin, it will print:
a. Winter Spring Summer Fall Ignored
b. Nothing at all, control passes to the first statement following the switch block
c. Winter
d. Ignored
Consider the following Java program segment:
int i =
int j =
int k =
int m =
int n =
m += i;
int x =
int y =
int z =
1;
1;
1;
0;
0;
int i=0;
do {
System.out.println(++i);
} while (i<10);
45. [2 pts] What numbers will be printed?:
a. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
b. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
c. 1, 2, 3, 4, 5, 6, 7, 8, 9
d. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
j++;
++k;
--i;
40. [2 pts] Following execution, the value of m will be:
e. 0
f. 1
g. 2
h. -1
41. [2 pts] Following execution, the value of x will be:
i. 0
j. 1
k. 2
l. -1
42. [2 pts] Following execution, the value of j will be:
m. 0
n. 1
o. 2
p. -1
43. [2 pts] Following execution, the value of y will be:
q. 0
r. 1
s. 2
t. -1
44. [2 pts] Following execution, the value of z will be:
u. 0
v. 1
w. 2
x. -1
51. [25 pts] Write a Java program to read 100 temperatures (as doubles) from the console
keyboard. Print the value of the lowest temperature read. Also print the average of all
the temperatures. The temperatures are in the Kelvin scale where 0.00 is the lowest
possible temperature. You will use built-in classes and methods including:
•
•
Scanner(System.in)
nextDouble()
Consider the following Java program segment:
//Scanner constructor for reading from keyboard
//Scanner method for reading the next double
46. [2 pts] The loop above is an example of:
a. Counter-controlled repetition
b. Sentinel-controlled repetition
c. Nested loops
d. All of the above
47. [2 pts] Statement: int counters[]; creates:
a. An array reference variable called int
b. An array reference variable called counters
c. An array of ten int!primitive!types
d. An array of ten counters
48. [2 pts] Statement: double x[][] = new double[10][10]; creates:
a. A single dimension array reference variable
b. A two dimension array reference variable
c. A single dimension array reference variable and an array of 10 elements
d. A two dimension array reference variable and an array of 100 elements
49. [2 pts] Every element in an array:
a. Must have the same data type
b. Must have the same value
c. Must occupy the same memory location
d. Must be a primitive data type
50. [2 pts] A continue statement inside the code block of a loop will:
e. Skip to the end of the loop’s code block and evaluate whether to continue
f. Continue with the first statement following the loop
g. Continue with the first statement following the enclosing method
h. Skip to the end of the loop’s code block and always continue looping
52. [25 pts] Write a Java program to calculate the sum all of the integers between 1 and 1000
inclusive, and print the result on the console (System.out):
53. [25 pts] Write a Java program to create a two-dimensional array of 100 by 100 elements,
each of type int, and set the value of every element in the array to 0 except for those on
the “main diagonal” (elements whose column number equals their row number) which
will be assigned the value 1.!!The!completed!array!will!thus!contain:!
1
0
.
.
.
0
0
1
.
.
.
0
…
…
.
.
.
…
0
0
.
.
.
1
54. [25 pts] In a particular course, students receive letter grades determined by their numeric
score as follows:
• A:
90 <= score <= 100
• B:
80 <= score < 90
• C:
70 <= score < 80
• D:
60 <= score < 70
• F:
score < 60
Write a Java program to calculate a student’s letter grade as a function of their numeric score
read from the keyboard with a Scanner. Print the student’s letter grade on the console.