CPSC 124 | Introduction to Programming | Spring 2024 |
Expect questions of the following types:
Write only what is asked for - a variable declaration, an assignment statement, an expression, and output statement, respectively. Also remember that the ending semicolon is part of the required syntax for a statement, but is not part of an expression.
Expect questions asking you to write Java for expressions involving relational and boolean operators, such as:
Also expect questions involving writing if statements, such as:
when x > 10 do System.out.println("big!");
when x > 10 do System.out.println("big!"); otherwise do System.out.println("small");
when x > 10 do System.out.println("big!"); otherwise when x > 3 do System.out.println("medium"); otherwise do nothing
Include only what is asked for - no variable declarations unless it says "declare a variable...", no assignment statements unless it says "store a value...", and so forth.
Expect questions of the form:
System.out.println("*");
System.out.println(x);
target = (int)(Math.random()*10);
target = (int)(Math.random()*10);
As indicated, you'll be provided with the code for the loop body. You may need to declare loop variables. Both while loops and for loops are acceptable.
Expect questions of the form:
In all cases, write just what is asked for — you do not need to declare or initialize any variables unless that is specifically what the question is asking for. For the "write an expression" questions, this is just the expression (e.g. numbers.length or numbers[3]) and not a full statement.
Expect questions of the form:
Write the header (comments and body not needed) for a subroutine to compute print the maximum of two integers: it should take two integers as parameters and print the larger of the two.
Write the header (comments and body not needed) for a function which computes the maximum of two integers: it should take two integers as parameters and return the larger of the two.
Write statement(s) which use the following to print the larger of 10 and the value stored in the variable x. You do not need to declare or initialize x, but you should declare and initialize any other variables you might use.
public static void printMax ( int a, int b ) { ... }
Write statement(s) which use the following to print the larger of 10 and the value stored in the variable x. You do not need to declare or initialize x, but you should declare and initialize any other variables you might use.
public static int max ( int a, int b ) { ... }
Expect questions of the form:
Complete the Java statement below to initialize deck with a new Deck of cards.
Deck deck = ...;
Let deck be a variable holding a Deck object. Write Java statement(s) to shuffle the deck.
Let deck be a variable holding a Deck object. Complete the Java statement below to initialize card with a card dealt from the deck.
Card card = ...;
Let card be a variable holding a Card object. Complete the Java statement below to print the suit and value of the card.
System.out.println("suit: "+...+", value: "+...);
You may be asked to work with the Card, Deck, and Hand classes from section 5.4, or with a new class. You will be provided with the javadoc (or similar) API of any classes you are to use (including Card, Deck, Hand).
Expect questions of the following form:
You are writing a class called Coin. What will be the class declaration? What will be the file name?
You are writing a class called Coin. Write a declaration for an instance variable named heads_ storing whether or not the coin is showing heads.
You are writing a class called Coin. It has a boolean instance variable heads_ storing whether or not the coin is showing heads. Write a default constructor which initializes the coin to be showing heads.
You are writing a class called Coin. It has a boolean instance variable heads_ storing whether or not the coin is showing heads. Write a constructor which takes a boolean parameter and sets whether or not the coin is showing heads to that value.
You are writing a class called Coin. It has a boolean instance variable heads_ storing whether or not the coin is showing heads. Write a getter named isHeads which returns whether or not the coin is showing heads.
You are writing a class called Coin. It has a boolean instance variable heads_ storing whether or not the coin is showing heads. Write a setter named setHeads which takes a boolean parameter and sets whether or not the coin is showing heads to that value.
You are writing a class called Coin. It has a boolean instance variable heads_ storing whether or not the coin is showing heads. Write a method named toss which tosses the coin, setting whether or not it is showing heads to a new random value.
If you can't access the links in the quiz:
Expect questions of the following form:
A class BankAccount has been defined. A minimum balance account is a kind of bank account. Write the class declaration for MinBalanceAccount. (Just the header, not the complete class.)
A class BankAccount has been defined. A minimum balance account is a kind of bank account. It has an account number, an owner, a balance>, and a minimum balance. Write the instance variable declarations(s) for MinBalanceAccount.
A class BankAccount has been defined. A minimum balance account is a kind of bank account. It has an account number account_, an owner owner_, a balance balance_, and a minimum balance minbalance_ and a constructor which takes all four values as parameters and initializes the instance variables accordingly. Write the the constructor for MinBalanceAccount (header and body).
A class BankAccount has been defined. A minimum balance account is a kind of bank account. It has an account number account_, an owner owner_, a balance balance_, and a minimum balance minbalance_. Money can be withdrawn from a minimum balance account under the same terms as a regular bank account, but additionally if the balance drops below the minimum balance, a $1 fee is charged. Fill in the blanks as indicated by the comments to define BankAccount's withdraw method.
public void withdraw ( double amount ) { // withdraw the amount using BankAccount's withdraw [blank] if ( balance_ < minbalance_ ) { balance_ -= 1; } }
BankAccount has two constructors:
public BankAccount ( int account, String owner, double balance ) { ... } public BankAccount ( int account, String owner ) { ... }
Write the body of second constructor so that it makes use of the first constructor to create a new bank account with the specific account number and owner, and a balance of 0.
A class BankAccount has been defined. A minimum balance account is a kind of bank account. It has an account number, an owner, a balance, and a minimum balance. There are two constructors, one which takes all four values as parameters and one which takes the account number, owner, and minimum balance. All four values can be retrieved, and the owner can be changed. Money can be deposited into or withdrawn from the account, but the balance can't drop below 0 and if the balance drops below the minimum balance, a $1 fee is charged. Which of the following will be included in the MinBalanceAccount class?
The final exam will be written (not on the computer) and closed book/notes. It will be cumulative, covering material from the whole course, and will focus on the fundamentals of Java syntax and semantics — how to write things and what it means when they appear in a program — as well as when to use those things. It will be split into two parts (syntax and the rest) and you will need to hand in part 1 before starting on part 2. You will have the full three-hour time period but the exam is not intended to be that long.
The topics covered will be the same as for the syntax quizzes — variables, assignment, expressions, output, conditionals, loops, arrays, subroutines and functions, using objects, writing classes, and inheritance — plus Javadoc-style comments and input using Scanner. Drawing and animation, ArrayList, and abstract classes, interfaces, and instanceof will not be on the exam.
Expect questions that ask you to write code as well as short answer "concept" questions and code-reading questions.
Code-writing questions will focus on syntax rather than algorithm development or problem solving. What you need to write will be more of a matter of translating pseudocode or fairly direct language rather than figuring out how to do something. Expect questions similar to the syntax quizzes, though they may not be as narrowly focused. For example, a question might ask you to write a subroutine (header and body) which takes an integer x as a parameter and prints out "big" if x is greater than 10 or "medium" if x is greater than 3 but less than 10, or to write a class or subclass given a list of its instance variables, constructor(s), and methods. You should understand the terminology used in the syntax quizzes. For example, if you are asked to write an expression, you should include only the expression and not extra. While the focus on these questions is on syntax, it won't be graded quite as strictly as on the quizzes — you won't lose points for the occasional missing semicolon, for example, but major syntax problems or a pattern of small errors (such as many missing semicolons) will count against you.
Code-reading questions might be tracing questions (asking you to do things like write what output is generated by a section of code when it is executed), questions which ask you to describe what happens when a section of code is executed, or questions which ask you to locate and fix syntax errors and/or logic bugs in a section of code. You should understand the core statements (variable declarations, assignments, conditionals, loops, subroutine/function calls, method calls), operators and expressions (arithmetic operators, relational operators, logical operators, creating new arrays and objects), and related aspects (such as that variables hold references to objects rather than the objects themselves, and the consequences of that when an object is passed as a parameter to a subroutine or function, an object is involved in an assignment statement, or when == is used).
Short answer concept questions might involve defining terms, explaining how something works or why the answer is what it is, recognizing when various Java constructs are applicable, ... — anything that isn't reading or writing code. Examples of definition and concept questions include, but aren't limited to:
An API will be provided if you are asked to use any classes.