CS56—Midterm Exam 2
E02, S13, Phill Conrad, UC Santa Barbara
05/21/2013
Name: ___________________________________________________
Umail Address: ______________________________@ umail.ucsb.edu
Circle one: 10am 11am
Please write your name only on this page.
That allows me to grade your exams without knowing whose exam I am grading.
This exam is closed book, closed notes, closed mouth, cell phone off,
except for:
- You are permitted one sheet of paper (max size 8.5x11") on which to write notes
- These sheets will be collected with the exam, and might not be returned
- Please write your name on your notes sheet
There are 100 points worth of questions on the exam, and you have 75 minutes to complete the exam.
A hint for allocating your time—on your first pass through the exam:
- if a question is worth 10 points, spend no more than 5 minutes on it
- if a question is worth 20 points, spend no more than 10 minutes on it
- if a question is worth 40 points, spend no more than 20 minutes on it
- etc.
If you do that, after you complete your first pass through the
exam in 50 minutes, you'll still have 25 minutes to:
- revisit any questions where you need more time
- check your work.
THROUGHOUT THIS EXAM: Keep your answers short and to the point, but detailed enough that if you offered them at a Job Interview, the interviewer would know, beyond a reasonable doubt, that you know what you are talking about.
The amount of space given is a hint to the length of answer I'm looking for. Short, and precise. Use Java-specific terms when you can.
When you can't remember the precise term, do your best to describe the concept in general terms, and hope for partial credit.
- (5 pts) Java has "automatic garbage collection". What does this mean?
- git allows for "branching" and "merging"
- (4 pts) What does it mean to "create a branch" in a git repository?
- (3 pts) Briefly describe a "real world programming" situation where creating a branch would be appropriate.
- (3 pts) Write the git command to create a new branch called "FredsBranch".
- (5 pts) In C++, a pointer can point to (and when dereferenced, refer to) an object on the stack, or an object on the heap.
Is the same with references in Java? Explain your answer.
-
Answer each of these questions in the context of Java Swing Programming.
- (10 pts) Suppose that you see that a particular GUI is built with a class FooComponent that extends JComponent and a class FooFrame that extends JFrame. With only that knowledge, explain what the relationship between FooComponent and FooFrame is likely to be, and what the role of each one is (i.e. what kind of code you'd expect to see inside each one.)
- (10 pts) Give two specific examples of Layout Managers, and describe briefly how each one works. You don't have to have the "exact" name—a close approximation (plus a reasonably accurate description of how it operates) is good enough. (Five points for each answer.)
- Name one layout manager here, then briefly describe: ___________________________
- Name one layout manager here, then briefly describe: ___________________________
- Continuing with questions about Java Swing. Keep answers short and precise.
- (5 pts) Explain briefly what kind of code you need to write so that when you click on a JButton, something happens—let's keep it simple, and suppose that what is supposed to happen is that "Hello" is printed on standard output. DON'T WRITE THE FULL CODE—just describe at a high level, using Java Swing specific terminology, how all the pieces fit together. You may use "snippets" of code if that helps, but I'm looking for a DESCRIPTION, not an implementation.
- (5 pts) Continuing from the previous problem—sometimes code that handles button presses and other GUI events involves the use of "inner classes". Explain how inner classes are used in this context, and why inner classes are helpful here.
- The Java standard library provides classes called Socket and Thread. For each of the items below, Briefly describe a "useful Java programming project" or a "real world Java program" that would need to use either Sockets, or Threads, and why Sockets or Threads are needed.
(Note an "assignment to learn about sockets/threads" is not an acceptable answer here. It needs to be a "real" kind of program.)
- (5 pts) Briefly describe a "useful Java program" that would need Sockets.
- (5 pts) Why would that need Sockets?
- (5 pts) Briefly describe a "useful Java program" that would need Threads
- (5 pts) Why would it need Threads?
- (10 pts) Write a line of Java code that declares a variable
kennel
, and set it equal to an ArrayList
that can only hold Dog
objects.
- (10 pts) Suppose a line of code inside method public void foo(); can throw an exception called FooException under some circumstances, and further more you know that FooException extends java.lang.Exception (rather than java.lang.RuntimeException).
In this situation, as the author of the code for the method, you must do one of two things, and it is up to you which one to do.
What are those two things?
Option 1:
Option 2:
- (10 pts) How would the situation described in the previous question be different if FooException extended java.lang.RuntimeException instead of java.lang.Exception, and even more important, why does Java have this whole other kind of exception?
How the situation would be different if FooException extended java.lang.RuntimeException?
(Hint: the "situation" is the SAME SITUATION as for the previous question, i.e. that you are the author of public void foo() EXCEPT that now FooException extends java.lang.RuntimeException. I am asking: what difference does that make in what you, as the author of the method public void foo must do?)
Why Java allows for this other kind of exception:
End of Exam
Total points: ?