Docsity

Exam 1 Multiple Choice Questions - Object Oriented Programming | COP 3330, Exams of Computer Science

Toggle sidebar
bg1
COP 3330 Exam #1
Multiple Choice Questions Form: A
2/13/07
1) Which of the following is NOT a primitive type in Java?
a) int b) double c) byte d) char e) none of the above
2) Which of the following defines the term "instance"?
a) one and only b) pertains to the object c) local variable
d) public method e) none of the above
3) Which of the following statements are true?
a) All classes in Java must have a main method.
b) All methods in a class must be public.
c) All instance variables in a class must be public.
d) A public class must be defined in a file with the same name as the class.
e) none of the above
4) What modifier indicates the user of a constant?
a) const b) final c) static d) int e) none of the above
5) What is the output of the following statement?
System.out.println("12+15 = "+12+15);
a) 12+15 = 27 b) 12+15 = 12+15 c) 12+15 = 1215
d) 27 = 27 e) none of the above
6) From which of the following can a Scanner object be used to read?
a) Keyboard b) text file c) .pdf file
d) both choices a and b e) both choices a and c
pf3
pf4
pf5

Partial preview of the text

Download Exam 1 Multiple Choice Questions - Object Oriented Programming | COP 3330 and more Exams Computer Science in PDF only on Docsity!

COP 3330 Exam

Multiple Choice Questions Form: A

  1. Which of the following is NOT a primitive type in Java? a) int b) double c) byte d) char e) none of the above
  2. Which of the following defines the term "instance"? a) one and only b) pertains to the object c) local variable d) public method e) none of the above
  3. Which of the following statements are true? a) All classes in Java must have a main method. b) All methods in a class must be public. c) All instance variables in a class must be public. d) A public class must be defined in a file with the same name as the class. e) none of the above
  4. What modifier indicates the user of a constant? a) const b) final c) static d) int e) none of the above
  5. What is the output of the following statement? System.out.println("12+15 = "+12+15); a) 12+15 = 27 b) 12+15 = 12+15 c) 12+15 = 1215 d) 27 = 27 e) none of the above
  6. From which of the following can a Scanner object be used to read? a) Keyboard b) text file c) .pdf file d) both choices a and b e) both choices a and c
  1. What is the output of the following program? public class q7 { public static void main(String[] args) { int x = 3, y=8; swap(x, y); System.out.println("x = "+x+", y = "+y); } public static void swap(int a, int b) { int temp = a; a = b; b = temp; } } a) a = 3, b = 8 b) a = 8, b = 3 c) x = 3, y = 8 d) x = 8, y = 3 e) none of the above
  2. What is the output of the following segment of code? String one = new String("one"); String two = "two"; two = one + one; System.out.println(one+two); a) three b) onetwo c) oneoneone d) oneoneoneone e) none of the above
  3. Which of the following properly opens a text file "input.txt" from which to read? a) Scanner fin = new Scanner("input.txt"); b) File fin = new File("input.txt"); c) Scanner fin = new File("input.txt"); d) Scanner fin = new Scanner(new File("input.txt")); e) none of the above
  4. What is the output of the following segment of code? for (int i=0; i<10; i++) { if (i%2 == 0) continue; System.out.print((2*i)+" "); } a) 0 1 2 3 4 5 6 7 8 9 b) 0 2 4 6 8 10 12 14 16 18 c) 1 3 5 7 9 11 13 15 17 19 d) 2 6 10 14 18 e) none of the above
  1. What is the output of the following segment of code? double temp = Math.pow(Math.E, 10); int temp2 = (int)(Math.log(temp)); System.out.println(temp2); a) 2.7182818 b) 0 c) 10 d) 1829784872 e) none of the above
  2. How many actual String objects are in existence at the end of the segment of code below, assuming that all possible garbage collection has already occurred? String a, b, c, d; a = "hello"; b = a; c = a.concat(b); d = b; c = c.substring(2); c = d; a) 0 b) 1 c) 2 d) 3 e) none of the above
  3. Assuming that c is a character variable that has a value, which of the following will return true if and only if c is a letter? a) isLetter(c) b) String.isLetter(c) c) c.isLetter() d) toLowerCase(c) e) none of the above
  4. Which of the following creates an ArrayList with a capacity of 50 elements? a) ArrayList mylist; b) ArrayList mylist = new ArrayList(50); c) ArrayList mylist = new ArrayList(); d) ArrayList mylist = new ArrayList[50]; e) none of the above
  5. The company that created Java shares its name with the star that fuels our Solar System with light. What is the name of the company? a) IBM b) Microsoft c) Sun Microsystems d) Apple e) none of the above

COP 3330 Exam #1 Free Response 2/13/ Name: __________________________________

  1. (15 points) Complete the main method below so that it reads in from the user a positive integer n, and then prints out the value of n! (Note: n! = 1x2x3…xn. So, 3! = 6, 4! = 24, 5! = 120, etc.) public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int n, ans=1; System.out.println("Enter the value of n."); System.out.println("n! = "+ans); }
  2. (15 points) Write a static method that takes in a String as a parameter and returns the number of characters in the String that are letters or digits. (Hint: Look at the methods in the Character class that start with "is.") Fill in the prototype below. public static int numLetDig(String s) { }
  1. (15 points) Write a code segment to do the following: a) Declare an array of Fraction references called harmonic of size 10. (1 line) b) Initialize the array with 10 newly created Fraction objects. (Note: You code will call the Fraction constructor 10 times.) These objects should have the value 1/2, 1/3, 1/4, … , 1/11. (a loop with one line inside) c) Declare a separate Fraction object named sum and initialize it to 0/1. (1 line) d) Calculate the sum of the 10 Fraction objects referenced by the array harmonic and store this sum in the Fraction object referenced by sum. (a loop with one line inside) d) Print this sum out. (1 line) You will only get full credit if you use a loop and DO NOT manually create the ten objects and manually add the ten objects.