Types, Variables and Expressions · Write code · 2 marks
The variable n holds a three-digit number (precondition: 100 n 999). Declare an int variable reversed holding the number with its digits in the opposite order. For example, if n is 482 then reversed is 284, and if n is 120 then reversed is 21. Do not use strings or loops.
int n = 482;int ones = n % 10;
int tens = n / 10 % 10;
int hundreds = n / 100;
int reversed = ones * 100 + tens * 10 + hundreds;For 120 the pieces are 0, 2 and 1, giving 0 + 20 + 1 = 21.
Static Methods, Parameters and Scope · Short answer · 2 marks
Write only the header (the first line, ending in {) of a public static method for each description. Do not write a body. You choose the names.
Takes a person’s age, a whole number, and tells whether it is from 13 to 19 inclusive.
Takes an array of whole numbers and gives back their mean, which may have a fractional part.
Takes a String and a number of repetitions and prints the string that many times; it gives nothing back.
public static boolean isTeen(int age) {
public static double average(int[] nums) {
public static void printBanner(String text, int times) {The return type comes from what the caller gets back: boolean for a yes/no answer, double for a mean, void when the method only prints. Each parameter needs its own type, including the array type int[]; the names may differ.
Arrays and Array Algorithms · Predict the output · 2 marks
Write the output of the code fragment below. If it does not compile, write COMPILE ERROR; if it throws an exception, write what is printed first, then the exception’s name.
int[] a = new int[5];
for (int i = 0; i < a.length; i++) {
a[i] = i * i;
}
a[a.length - 1] = a[1] + a[2];
System.out.println(Arrays.toString(a));
int total = 0;
for (int x : a) {
total += x;
}
System.out.println(total);
System.out.println(a.length);[0, 1, 4, 9, 5]
19
5The first loop stores 0, 1, 4, 9, 16; the assignment then replaces the last element (16) by a[1] + a[2], which is 5. The sum is 0 + 1 + 4 + 9 + 5 = 19, and the length does not change.
Inheritance, Interfaces and Polymorphism · Multiple choice · 2 marks
Consider these declarations.
interface Flyer { void fly(); }
interface Swimmer { void swim(); }
class Duck implements Flyer, Swimmer {
public void fly() { }
public void swim() { }
}
Flyer f = new Duck();Select all that apply. Circle ALL of the following statements that compile when placed after the code above.
Object o = f;
Duck d = (Duck) f;
Swimmer s = f;
Flyer g = new Flyer();
Swimmer s = (Swimmer) f;
Answer: (A, B, E)
Every object is an Object, so the first line is an upcast. A cast from an interface type to a class that implements it ((Duck) f) or to another interface ((Swimmer) f) is accepted by the compiler, which cannot rule out that the object is a Duck; at run time it succeeds because the object is one. Without a cast, Swimmer s = f; fails: the declared type Flyer is not a Swimmer. An interface can never be instantiated with new.
Recursion, Searching and Sorting · Multiple choice · 2 marks
A pass of bubbleSort is one iteration of its outer for loop. The method swap(a, i, j) exchanges a[i] and a[j].
public static void bubbleSort(int[] a) {
for (int end = a.length - 1; end > 0; end--) {
for (int i = 0; i < end; i++) {
if (a[i] > a[i + 1]) {
swap(a, i, i + 1);
}
}
}
}The method is called on the array {6, 3, 5, 1, 4}. What does the array contain after exactly two passes?
{3, 5, 1, 4, 6}
{3, 5, 6, 1, 4}
{1, 3, 5, 6, 4}
{3, 1, 4, 5, 6}
Answer: (D)
(D) Pass 1 carries the 6 to the end: {3, 5, 1, 4, 6}. Pass 2 (which stops before the last cell) carries the 5 up to just before the 6: {3, 1, 4, 5, 6}. The first choice is the array after only one pass; the second is what insertion sort gives after two passes, and the third is what selection sort gives after two passes.
Introduction to Programming · The University of Texas at Austin (UT Austin, UT, UTexas) · also written CS312
Make a free, printable C S 312 practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 400 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UT Austin library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh C S 312 practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with The University of Texas at Austin. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.