0% found this document useful (0 votes)
64 views

CSE-1020 Final Exam

This document appears to be an exam for a computer science course covering topics like Java language constructs, strings, regular expressions, collections, inheritance, exceptions, and validation. It provides instructions for taking the exam, including allowing 80 minutes to complete it and not permitting aids. The exam is divided into 5 sections worth a total of 50 points. Each section contains multiple-choice or short answer questions testing understanding of the listed computing topics.

Uploaded by

examkiller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

CSE-1020 Final Exam

This document appears to be an exam for a computer science course covering topics like Java language constructs, strings, regular expressions, collections, inheritance, exceptions, and validation. It provides instructions for taking the exam, including allowing 80 minutes to complete it and not permitting aids. The exam is divided into 5 sections worth a total of 50 points. Each section contains multiple-choice or short answer questions testing understanding of the listed computing topics.

Uploaded by

examkiller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

18 December 2009 CSE-1020 Final Exa Fall 2009 p.

1 of 12
CSE-1020 Final Exam
Written Portion
Family Name:
Given Name:
Student#:
CSE Account:
Section: A E (Circle which section you are in.)
Instructors: Parke Godfrey (A) & Burton Ma (E)
Exam Duration: 80 minutes
Term: Fall 2009
Instructions
1. Write your answers clearly and succinctly.
(a) You will receive no point for an unclear answer.
(b) There is no additional deduction for a wrong answer.
(c) Do not use red ink.
2. No aids (such as calculator, reference sheets, textbooks, etc.) are permitted.
3. Please turn o cell phones, and put your cell phones o the desk.
4. Generally, no questions regarding the interpretation, intention, or further elucidation of an
exam question will be answered by invigilators.
If truly in doubt, state your interpretation next to your answer.
5. For any of the program code, assume that
(a) any program fragment is properly within a main method within a class,
(b) any needed classes have been imported,
(c) any constructor call has a legitimate matching constructor, and
(d) that any unseen part of the program is correct.
Thus, it would compile and run, except perhaps because of the program fragment shown.
Grading Box
1. /10
2. /10
3. /10
4. /10
5. /10
Total /50
18 December 2009 CSE-1020 Final Exam w/ answersFall 2009 p. 2 of 12
1. Language Constructs. [10pts]
Each question (a)(j) refers to a line of code in the API and main from page 3. Match that
line from the code to the concept on the right (by its letter, AL) that it best illustrates.
Note that no concept, AL, is a best answer more than once. Also note that there are twelve
concepts, AL, but just ten questions, (a)(j). So two concepts will go unused.
(a) Line 3
(b) Line 5
(c) Line 10
(d) Line 12
(e) Line 14
(f) Line 15
(g) Line 16
(h) Line 26
(i) Line 28
(j) Line 29
A. accessor
B. aggregation
C. constant
D. delegation
E. inheritance
F. generics
G. hibernation
H. mutator
I. polymorphism
J. promotion
K. overriding
L. shadowing
18 December 2009 CSE-1020 Final Exam w/ answersFall 2009 p. 3 of 12
The following code shows the API of two classes Mammal and Dog, and a class for the app Zoo
which imports and uses Mammal.
1 public class Mammal
2 {
3 public final int EYES = 2;
4 public int mass;
5 public Date birthday;
6
7 public int eat(int x); // Returns 2 * x.
8 }
9
10 public class Dog extends Mammal
11 {
12 public long mass;
13
14 public int getAge(); // Returns the value of age.
15 public void setAge(int x); // Changes the value of age.
16 public int eat(int x); // Returns 7 * x.
17 public int eat(double x); // Returns (int)(7 * x).
18 }
19
.
.
.
20 import java.util.*;
21
22 public class Zoo
23 {
24 public static void main(String[] args)
25 {
26 Set<Mammal> specimens = new HashSet<Mammal>();
27 int x = 3;
28 double y = x;
29 int z = Integer.parseInt("416");
30 }
31 }
18 December 2009 CSE-1020 Final Exam w/ answersFall 2009 p. 4 of 12
2. Strings and Regular Expressions. [10pts]
(a) [2pts] Consider the following code.
1 String bookTitle = "The Count of Monty Python";
2 bookTitle.toLowerCase(); // Cast to lowercase.
3 System.out.println(nookTitle);
i. [1pt] What is immutability for String?
ii. [1pt] What does the code above print?
(b) [2pts] A programmer has written the following, sentinel-based input loop. It is sup-
posed to read input from the keyboard using a Scanner object named input, until the
user enters the string "done" or "DONE".
1 String userInput = input.next();
2 while (userInput != "done" && userInput != "DONE")
3 {
4 do something
5 }
The program compiles and runs, but the loop body executes even when the user enters
the sentinel string!
i. [1pt] Explain what is wrong with the programmers code.
ii. [1pt] Provide a x (solution) for the problem.
18 December 2009 CSE-1020 Final Exam w/ answersFall 2009 p. 5 of 12
(c) [4pts] A programmer is using the xed-length codes technique to convert York letter
grades (A, B, C, D, E, F) to numeric GPA values (8, 6, 4, 2, 1, 0).
1 final String LETTERS = "ABCDEF";
2 final String GPA = "864210";
3 String grade = null;
4 Assume some code here that assigns a valid letter-grade to grade.
5
.
.
.
i. [2pts] Add one to three lines of Java code at line 5 onward to store in a variable
of the appropriate type the correct numeric GPA value corresponding to grades
value. E.g., if grade = B, then the GPA value is 6.
ii. [1pt] What happens if grades value is a string of length one, but it does not
correspond to one of the valid letter values?
iii. [1pt] Suggest one way to deal with this problem. (Your answer does not need to
include any Java code.)
(d) [2pts] Write a regular expression that will match a sequence of one or more lowercase
English letters, and nothing else.
18 December 2009 CSE-1020 Final Exam w/ answersFall 2009 p. 6 of 12
3. Collections. [10pts]
For each of the following,
state what the program will print, or
state that the program fails with a compile-time error, and clearly state why.
(a) [2pts]
1 Set<Integer> nums = new TreeSet<Integer>();
2 nums.add(5);
3 nums.add(10);
4 nums.add(2);
5 nums.add(5);
6
7 System.out.println("[" + nums.size() + "]");
8 for (Integer i : nums)
9 {
10 System.out.println(i);
11 }
(b) [2pts]
1 Map<String, String> m = new HashMap<String, String>();
2 m.put("LOL", "laugh out loud");
3
4 for (String s : m.values())
5 {
6 System.out.println(s);
7 }
18 December 2009 CSE-1020 Final Exam w/ answersFall 2009 p. 7 of 12
(c) [2pts]
1 Collection<RewardCard> cards = new ArrayList<RewardCard>();
2 cards.add(new CreditCard(1001, "Bob") );
3
4 System.out.println(cards.size());
(d) [2pts]
1 List<Character> letters = new ArrayList<Character>();
2 letters.add(C);
3 letters.add(B);
4 letters.add(A);
5
6 for (int i = 0; i < 3; i++)
7 {
8 System.out.println(letters.get(i));
9 }
(e) [2pts]
1 Set<String> s = new HashSet<String>();
2 s.add("A");
3 s.add("B");
4 s.add("C");
5
6 System.out.println(s.get(1));
18 December 2009 CSE-1020 Final Exam w/ answersFall 2009 p. 8 of 12
4. Inheritance. [10pts]
Refer to the API for Mammal and Dog on page 3. For each code question, state what each
prints, or that it fails at compile-time or run-time and why.
(a) [5pts]
i. [1pt]
1 Dog fred = new Dog();
2 System.out.println(fred.EYES);
ii. [1pt]
1 Dog fred = new Dog();
2 System.out.println(fred.eat(3.2));
iii. [1pt]
1 Mammal fred = new Dog();
2 System.out.println(fred.eat(3));
iv. [1pt]
1 Mammal fred = new Dog();
2 System.out.println(fred.eat(3.2));
v. [1pt]
1 Dog fred = new Dog();
2 System.out.println(fred.setAge(5).getAge());
18 December 2009 CSE-1020 Final Exam w/ answersFall 2009 p. 9 of 12
(b) [5pts]
i. [1pt] What kind of relationship between classes does inheritance describe?
ii. [1pt]
1 Dog fred = new Dog();
2 System.out.println(fred instanceof Mammal);
iii. [1pt] Say whether the following is correct, or explain why it is incorrect.
1 Map<Dog, int> pairs = new HashMap<Dog, int>();
2 Dog greg = new Dog();
3 pairs.add(greg, 7);
iv. [2pts] What does substitutability mean in Java?
18 December 2009 CSE-1020 Final Exam w/ answersFall 2009 p. 10 of 12
5. Exceptions & Validation. [10pts]
Circle the letter corresponding to the one best answer for each.
(a) [2pts] A client must write code to deal with what kind of exceptions?
A. Exception and its subclasses.
B. Error and its subclasses.
C. RuntimeException and its subclasses.
D. Checked exceptions.
E. None.
(b) [2pts] What kind of exceptions can usually be prevented from being thrown if the
programmer performs input validation?
A. Exception and its subclasses.
B. Error and its subclasses.
C. RuntimeException and its subclasses.
D. Checked exceptions.
E. None.
(c) [2pts] What output does the following code fragment produce?
1 for (int i = 3; i >= 0; i--)
2 {
3 try
4 {
5 int quotient = 6 / i;
6 System.out.print(quotient);
7 }
8 catch (ArithmeticException ex)
9 {
10 System.out.println(0);
11 }
12 }
A. 236
B. 2360
C. 0
D. Compile-time error.
E. Run-time error.
18 December 2009 CSE-1020 Final Exam w/ answersFall 2009 p. 11 of 12
(d) [2pts] What output does the following code fragment produce?
1 try
2 {
3 System.out.print("a");
4 throw new RuntimeException("Arghh!");
5 }
6 catch(Exception err)
7 {
8 System.out.print("b");
9 }
10 System.out.println("c");
A. ac
B. bc
C. abc
D. Compile-time error.
E. Run-time error.
(e) [2pts] What output does the following code fragment produce?
(NullPointerException is a subclass of RuntimeException.)
1 try
2 {
3 throw new NullPointerException();
4 }
5 catch (ArithmeticException ex)
6 {
7 System.out.println("ArithmeticException!");
8 }
9 catch (IndexOutOfBoundsException ex)
10 {
11 System.out.println("IndexOutOfBoundsException!");
12 }
13 catch (NoSuchElementException ex)
14 {
15 System.out.println("NoSuchElementException!");
16 }
A. ArithmeticException!
B. IndexOutOfBoundsException!
C. NoSuchElementException!
D. Compile-time error.
E. Run-time error.
18 December 2009 CSE-1020 Final Exam w/ answersFall 2009 p. 12 of 12
Blank Page.
Relax. Breathe. Turn in your exam.

You might also like