LMU | CMSI 185
COMPUTER PROGRAMMING
Practice Questions
  1. Match the terms with their definitions, by writing the letter of the definition next to the term.
    1. Expression _______
    2. Variable _______
    3. Statement _______
    4. Application _______
    5. Subroutine _______
    6. Loop _______
    7. Exception _______
    8. Argument _______
    9. % _______
    10. Applet _______
    11. Enum _______
    12. Block _______
    13. boolean _______
    14. Graphics _______
    15. Computing _______
    1. An object that you throw to signal that something is wrong
    2. The primitive type containing only the values true and false.
    3. The science of information processes
    4. A named storage location
    5. A sequence of statements
    6. A user-defined type containing a fixed number of elements
    7. In Java, an object used for drawing
    8. The mathematical remainder operator
    9. A parameterized chunk of code that can be run on demand
    10. A program designed to run inside of another (often a web browser)
    11. A chunk of code that is evaluated
    12. Code that produces an action
    13. Code that is executed repeatedly
    14. A value passed to a subroutine
    15. A standalone program
  2. Here is an attempt to write a program that computes the sum of the numbers from 1 to 10000, inclusive. However, there are three major mistakes. Identify the mistakes and explain how to correct them. (Note: find actual programming mistakes, not mistakes of style; do not pick on the names, the indentation, the formatting, or the lack of comments.)
    public class Summer {
        public static void main(String[] args) {
            int sum;
            for (int i = 1; i < 10000; i++) {
                sum += i;
                System.out.println("The sum from 1 to 10000 is " + sum);
            }
        }
    }
    
  3. Write a complete Java program that displays a string made up of the first characters of each of the program's (command line) arguments. For example, if you ran the program with the four arguments Rats are very intelligent your program should display Ravi.
  4. Write a subroutine — just a subroutine, not a complete Java program — to determine whether a given year is a leap year or not. Throw an IllegalArgumentException if the year is earlier than 1582. Note: A year is a leap year if it divisible by 4, but not 100, unless also by 400. Got that? English can be confusing.

    To help you get started, here is the first line of the subroutine. (Please note that you are being asked to do the leap year computation on your own, and not via the GregorianCalendar class in the Java library.)

    public static boolean isLeapYear(int year) {
    
  5. Write a complete Java program that asks the user for a string via showInputDialog, then displays the Fuddified version of the string with showMessageDialog. (The Fuddified version of a string is the string with all r's and l's replaced with w's.)
  6. Write the paint method for a Java applet that draws 20 randomly placed squares, each 30 pixels wide. The overall canvas size for the applet should be 400 x 300. Make sure each square fits completely in the canvas. The squares can be any color you choose. I've started the applet for you; again, just write the paint method.
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.util.Random;
    
    public class RandomSquareApplet extends Applet {
    
        private static final int WIDTH = 400;
        private static final int HEIGHT = 300;
        private static final int SIDE_LENGTH = 30;
        private static final int NUMBER_OF_SQUARES = 20;
        private static Random randy = new Random();
    
        public void paint(Graphics g) {
    
  7. If you've been reading the textbook for this class, you will have come across the "3n+1" sequence. This is a sequence of non-negative numbers determined by the rule:
        Call the current value n
        If n == 1, the sequence is done
        Else if n is odd, the next value is 3*n + 1
        Else the next value is n/2
    
    Write a subroutine — just a subroutine, not a complete program — that accepts an integer and returns the length of the 3n+1 sequence for that initial value. Some examples:
        lengthOf3nPlus1(1) == 1
        lengthOf3nPlus1(3) == 8
        lengthOf3nPlus1(4) == 3
        lengthOf3nPlus1(16) == 5
    

    Psst: Why do you think the word "non-negative" appeared in bold in the problem description?