Exam 2 (Mar 11)
Answer the sample exam questions on your own so you know what you need to study.
Sample Questions
Take the exam below on your own, closed book, without a computer. Set a 50-minute timer so you can practice finishing in the time alloted. The actual exam will be similar in difficulty and length to this exam.
-
These specific questions should not be construed as a study guide. The questions do not necessarily provide information about what you should and/or shouldn’t study. Try to write and answer different variants of each question. You may discuss your questions and answers with other students enrolled in the course.
-
The answers to sample exams will not be made available. Of course, you can easily check your answers to many questions by editing, compiling, and/or executing the code. This is to help you avoid focusing too much attention on the specifics of these questions.
Tip
Practice drawing memory diagrams for Question 2 (EarRing, LipRing, NavelRing, and NoseRing) of the sample exam.
Memory Diagrams
Activity 8 and the Memory Diagrams tutorial may be helpful for reviewing the basics. For more practice, draw a memory diagram for each of the exercises below.
Exercise 1: Variables
long money = 50;
boolean good = true;
double price = money;
char hash = '#';
String rating = "PG";
Exercise 2: Arrays
int[] data = null;
int[] counts = {10, 3, 7, -5};
double[] scores = new double[3];
String[] words = new String[2];
String[] take = {"CS 159", "CS 227"};
int[] easy = {1, 2, 3};
int[] copy = easy;
Exercise 3: Methods
public static void main(String[] args) {
int[] nums = {159, 227};
printArray(nums);
}
public static void printArray(int[] a) {
System.out.print("{" + a[0]);
for (int i = 1; i < a.length; i++) {
System.out.print(", " + a[i]);
}
System.out.println("}");
}
Exercise 4: Objects
public class Card {
private int rank; // 1=Ace, ..., 11=Jack, 12=Queen, 13=King
private int suit; // 0=Clubs, 1=Diamonds, 2=Hearts, 3=Spades
public Card(int rank, int suit) {
this.rank = rank;
this.suit = suit;
}
public static void main(String[] args) {
Card c1 = new Card(8, 1);
Card c2 = new Card(12, 2);
Card c3 = c1;
c1 = c2;
}
}
Exercise 5: Static
public class BankAccount {
private static final String PREFIX = "1234";
private static int nextNumber = 1;
private String number;
private String owner;
private double balance;
public BankAccount(String owner) {
this.number = PREFIX + String.format("%04d", nextNumber);
this.owner = owner;
nextNumber++;
}
public static void main(String[] args) {
BankAccount ba1 = new BankAccount("Stacie");
BankAccount ba2 = new BankAccount("Trevor");
}
}
Video 1: Classes
This video by Dr. Weikle was posted before Exam 1 but is also a great review of HW5 topics.
Video 2: Exceptions
Note: This video is from two years ago. File I/O is not on this year’s Exam 2. Feel free to watch the first half, stopping at 4:45. Or watch the entire video if you would like to see next week’s topic.