Sunday, March 31, 2013

AP Computer Science Study Guide

AP Computer Science Study Guide

AP Computer Science Study Guide

Introduction

Java is a good language for programming. It has a wealth of features and large libraries of code for almost anything you could want to do with a program. Had I had a year with a class I would have endeavored to teach them as much of Java as I could. Luckily for AP Computer Science students I am not in charge of the test. Instead you have a much smaller scope of knowledge to deal with. I have said before you should download the AP course description from the College Board website. It details exactly what subject matter you should be familiar with.

Test Taking Strategy

There are no point reductions for wrong answers so you should strive to answer all the multiple choice questions. With 5 minutes left to go you should stick in any answer in the blank problems then go back and see how many you can answer. Every question should have an answer! Don't take so much time that you have blanks on your computer answer sheet.

You have 2 minutes per problem (less really). You can't get stuck for 5 minutes on any one problem. You should quickly go through the test and find all questions you can answer quickly and easily. The worst thing you can do is get stuck on a problem and leave 5 easy questions unanswered at the end of the test.

When you take practice tests you should compile a list of which type of problems take you the longest and which type are you more inclined to get wrong. Then save these for last. Once you realize a question falls into that category guess an answer (put down D or E or something) don't even do an educated guess. Mark the problem to come back to and continue. Once you get through the test the first time now go back and answer the questions you had to guess on. Now you can take a little bit more time on each problem. You will be confident that you have found all the easy "get-able" questions and now you are just working to improve your score.

DANGER: Don't missalign problems in your bubble sheet because you skipped them. You should certainly practice this method on a couple of practice tests before you do it on the real exam. By practice I mean get a bubble sheet and take the test on the bubble sheet then check your answers to make sure you did not misalign anything.

The AP Computer Science Java Subset

Base types

  • int, double, boolean

Arithmetic operators

OperatorName
+addition
-subtraction
*muliplication
/division
%modulo

Comparison Operators

OperatorNameFunction
==equalstest if equal to
!=not equalstest if not equal to
<less thantest if left value 'less than' right value
<=less than or equal totest if left 'less than or equal to' right
>greater thantest if left 'greater than' right
>=greater than or equal totest if left 'greater than or equal to' right

Logical Comparisons

OperatorNameFunction
&&andreturns true if both left and right values are true otherwise it returns false
||orreturns false if both left and right values are false otherwise it returns true
!notreturns the opposite of the right hand value true becomes false and vise versa
NOTE: The && and || 'short circuit' because of the fact that any false value in && makes the whole statement false further processing does not have to occur the compiler can immediately choose the else clause. The same but opposite happens for || except as soon as the compiler finds a true value it can go into the next statement in the if portion of an if-then-else statement

Assignment Operators

OperatorFunction
=assign value of right hand side (RHS) to variable on left hand side (LHS)
+=LHS = LHS + RHS
-=LHS = LHS - RHS
*=LHS = LHS * RHS
/=LHS = LHS / RHS
%=LHS = LHS % RHS
(int)truncates double giving the int portion: int myvar = (int) 32.25 //myvar = 32
(double)makes a double value out of an int value so +-*/ operations return double values

The Study Guide in Code

To help you study for the AP exam I have gone through the subset and placed comments for each of the subset items. Then for a portion of it I have implemented examples of the subset. To save some typing I have implemented 'log' methods to simplify the output. Between the code and the output you should have a pretty good idea of what the subset entails and what you are responsible to know. Hopefully I will have time to provide you a completed sheet that way you may have some extra examples of code going into the AP test.

Don't forget to check out the updated version in the blog post at the following link:
Study Guide take 2

/*
 * This Class definition is to give an example of using everything in 
 * the Java subset used on the AP exam. Use this as a learning tool and 
 * add in code from test questions where you weren't sure how the compiler
 * would handle the code. It will become your study guide for the AP exam.
 * This is not a substitute for reading the course pdf file on the College
 * Board site. 
 */
package apcompsci;

/**
 *
 * @author nasty old dog
 */
public class APSubset {

    public static void log(String str)
    {
        System.out.print("APSubset: ");
        System.out.println(str);
    }

    public static void log_array(String str, int[] arr)
    {
        System.out.print("APSubset: ");
        System.out.print(str);
        for (int i : arr){
            System.out.print(i + "  ");
        }
        System.out.println();
    }

    public static void main (String[] args)
    {
        // Basic types: int, double, boolean
        int a = 0;
        double b = 0.0;
        boolean c = false;
        log("BASIC TYPES");
        log("int a = " + a);
        log("double b = " + b);
        log("boolean c = " + c);

        // Basic operators: +, -, *, /, %
        log("\n");
        log("BASIC OPERATORS");
        a = 1 + 2;
        log("int a = 1 + 2 = " + a);
        b = 2.0 + 3.5;
        log("double b = 2.0 + 3.5 = " + b);
        a = 2 - 3;
        log("int a = 2 - 3 = " + a);
        b = 2.0 - 3.5;
        log("double b = 2.0 - 3.5 = " + b);
        a = 2 * 3;
        log("int a = 2 * 3 = " + a);
        b = 2.0 * 3.5;
        log("double b = 2.0 * 3.5 = " + b);
        a = 3 / 2;
        log("int a = 3 / 2 = " + a);
        b = 3.5 / 2.0;
        log("double b = 3.5 / 2.0 = " + b);
        a = 17 % 5;
        log("int a = 17 % 5 = " + a);
        b = 17.0 % 6.0;
        log("double b = 17.0 % 6.0 = " + b);

        // To show modulo(remainder) lets show the division first

        // preincrement/postincrement predecrement/postdecrement
        // These are used for their side effects and never inside an expression
        // mainly for loop increment/decrement
        log("\n");
        log("Pre/Post-increment and Pre/Post-decrement");
        log("int a = " + a);
        log("a++ = " + a++);
        log("int a = " + a);
        log("++a = " + ++a);
        log("int a = " + a);
        log("a-- = " + a--);
        log("int a = " + a);
        log("--a = " + --a);
        log("int a = " + a);

        log("double b = " + b);
        log("b++ = " + b++);
        log("double b = " + b);
        log("++b = " + ++b);
        log("double b = " + b);
        log("b-- = " + b--);
        log("double b = " + b);
        log("--b = " + --b);
        log("double b = " + b);

        // Assignment operators: +=, -=, *=, /=, %=
        log("\n");
        log("Assignment operators +=, -=, *=, /=, %=");
        a = 13;
        log ("int a = " + a);
        b = 13.0;
        log ("double b = " + b);
        a += 7;
        log ("int a += 7 = " + a);
        b += 7.5;
        log ("double b += 7.5 = " + b);
        a -= 3;
        log ("int a -= 3 = " + a);
        b -= 3.5;
        log ("double b -= 3.5 = " + b);
        a *= 2;
        log ("int a *= 2 = " + a);
        b *= 2.0;
        log ("double b *= 2.0 = " + b);
        a /= 2;
        log ("int a /= 2 = " + a);
        b /= 2.0;
        log("double b /= 2.0 = " + b);
        a %= 5;
        log("int a %= 5 = " + a);
        b %= 6.0;
        log("double b %= 6.0 = " + b);

        // Relational operators: == != < <= > >=
        c = 1 == 1;
        log("boolean c = 1 == 1 = " + c);
        c = 2 != 3;
        log("boolean c = 2 != 3 = " + c);
        c = 2 < 1;
        log("boolean c = 2 < 1 = " + c);
        c = 2 < 3;
        log("boolean c = 2 < 3 = " + c);
        c = 2 <= 4;
        log("boolean c = 2 <= 4 = " + c);
        c = 2 <= 2;
        log("boolean c = 2 <= 2 = " + c);
        c = 4 > 1;
        log("boolean c = 4 > 1 = " + c);
        c = 4 >= 5;
        log("boolean c = 4 >= 5 = " + c);
        c = 5 >= 5;
        log("boolean c = 5 >= 5 = " + c);
        // Logic operators || && ! short circuit evaluation
        log("\n");
        log("Logical Operators ||, &&, !");
        c = true || true;
        log("boolean c = true || true = " + c);
        c = true || false;
        log("boolean c = true || false = " + c);
        c = false || false;
        log("boolean c = false || false = " + c);
        c = true && true;
        log("boolean c = true && true = " + c);
        c = true && false;
        log("boolean c = true && false = " + c);
        c = false && false;
        log("boolean c = false && false = " + c);
        int d[] = null;
        if (d != null && d[1]==2) {
            // The d[1] is an illegal operation ArrayOutofBounds error
            // But because d == null it never gets executed due to the 
            // short circuit
        }
        else {
            log("int[] d = null: in else clause due to short circuit");
        }
        d = new int[] {1,2,3,4,5};
        if (d != null && d[1]==2) {
            // The d[1] is an illegal operation ArrayOutofBounds error
            // But because d == null it never gets executed due to the 
            // short circuit
            log_array("int[] d = ",d);
        }
        // numeric casts (int) and (double))
        b = 5.0 / 2;
        log("double b = 5.0/2 = " + b);
        b = (int) b;
        log ("double b = (int) b = " + b);
        a = (int) 5.0/2;
        log ("int a = (int) 5.0/2 = " + a);
        b = 5.0 / (int) 2;
        log("double b = 5.0/(int) 2 = " + b);
        b = (int) 5.0 / (int) 2;
        log("double b = (int) 5.0/(int) 2 = " + b);
        b = ((int) 5.0) / (double) 2;
        log("double b = ((int) 5.0)/(double) 2 = " + b);
        b = ((int) 5.0) / 2;
        log("double b = ((int) 5.0)/ 2 = " + b);

        // String concatenation
        // String escape sequences: \\, \n, \"
        // One Dimensional Arrays
        // Two Dimensional Arrays
        // if stm
        // if-else stm
        // while
        // for
        // for each
        // method over loading
        // new operator
        // public and private
        // java comments /* // and /** @param and @return
        // final for variables and final class scope
        // static methods static variables
        // null
        // this forget what I told you they don't care about it.
        // Super
        // Initialize all variables in constructors implicit initialization is not in subset
        // extend classes and implement interfaces method overriding and polymorphism
        // interface and abstract classes
        // difference between = and ==
        // Conversion from Subclass to Superclass and Class casts from Object to another class
        // import specific classes
        // Generic Class (need to know how it works won't be required to implement)
        // String.compareTo()
        // Exceptions
        // Standard Classes: String, Math, List, ArrayList
        // Wrapper classes: Integer, Double
        // Class java.lang.Object: equals(Object other) toString()
        // Class java.lang.Integer
        // Class java.lang.Double
        // Class java.lang.String
        // Class java.lang.Math
        // Interface java.util.List<E>
        // Interface java.util.ArrayList<E>
    }
}

Output

run:
APSubset: BASIC TYPES
APSubset: int a = 0
APSubset: double b = 0.0
APSubset: boolean c = false
APSubset: 

APSubset: BASIC OPERATORS
APSubset: int a = 1 + 2 = 3
APSubset: double b = 2.0 + 3.5 = 5.5
APSubset: int a = 2 - 3 = -1
APSubset: double b = 2.0 - 3.5 = -1.5
APSubset: int a = 2 * 3 = 6
APSubset: double b = 2.0 * 3.5 = 7.0
APSubset: int a = 3 / 2 = 1
APSubset: double b = 3.5 / 2.0 = 1.75
APSubset: int a = 17 % 5 = 2
APSubset: double b = 17.0 % 6.0 = 5.0
APSubset: 

APSubset: Pre/Post-increment and Pre/Post-decrement
APSubset: int a = 2
APSubset: a++ = 2
APSubset: int a = 3
APSubset: ++a = 4
APSubset: int a = 4
APSubset: a-- = 4
APSubset: int a = 3
APSubset: --a = 2
APSubset: int a = 2
APSubset: double b = 5.0
APSubset: b++ = 5.0
APSubset: double b = 6.0
APSubset: ++b = 7.0
APSubset: double b = 7.0
APSubset: b-- = 7.0
APSubset: double b = 6.0
APSubset: --b = 5.0
APSubset: double b = 5.0
APSubset: 

APSubset: Assignment operators +=, -=, *=, /=, %=
APSubset: int a = 13
APSubset: double b = 13.0
APSubset: int a += 7 = 20
APSubset: double b += 7.5 = 20.5
APSubset: int a -= 3 = 17
APSubset: double b -= 3.5 = 17.0
APSubset: int a *= 2 = 34
APSubset: double b *= 2.0 = 34.0
APSubset: int a /= 2 = 17
APSubset: double b /= 2.0 = 17.0
APSubset: int a %= 5 = 2
APSubset: double b %= 6.0 = 5.0
APSubset: boolean c = 1 == 1 = true
APSubset: boolean c = 2 != 3 = true
APSubset: boolean c = 2 < 1 = false
APSubset: boolean c = 2 < 3 = true
APSubset: boolean c = 2 <= 4 = true
APSubset: boolean c = 2 <= 2 = true
APSubset: boolean c = 4 > 1 = true
APSubset: boolean c = 4 >= 5 = false
APSubset: boolean c = 5 >= 5 = true
APSubset: 

APSubset: Logical Operators ||, &&, !
APSubset: boolean c = true || true = true
APSubset: boolean c = true || false = true
APSubset: boolean c = false || false = false
APSubset: boolean c = true && true = true
APSubset: boolean c = true && false = false
APSubset: boolean c = false && false = false
APSubset: int[] d = null: in else clause due to short circuit
APSubset: int[] d = 1  2  3  4  5  
APSubset: double b = 5.0/2 = 2.5
APSubset: double b = (int) b = 2.0
APSubset: int a = (int) 5.0/2 = 2
APSubset: double b = 5.0/(int) 2 = 2.5
APSubset: double b = (int) 5.0/(int) 2 = 2.0
APSubset: double b = ((int) 5.0)/(double) 2 = 2.5
APSubset: double b = ((int) 5.0)/ 2 = 2.0
BUILD SUCCESSFUL (total time: 2 seconds)

Conclusion

You shouldn't wait for me to finish this. Go ahead and take on the task yourself. By implementing each part of the Java AP subset you will further enhance your knowledge. In previous articles I have deviated from the subset to help you understand some basic programming concepts and constructs. But you should start to tailor your studying specifically for the AP (at this time the AP is about a month away for 2013).

If you haven't started doing practice test you should start. Anycode you had problems with you should add to this study guide. By the time you get through 3 or 4 tests you should be an expert on the AP subset and know exactly what to expect in May. Remember in the free response questions to limit yourself to the AP subset. If your course was taught properly you should have learned more Java than you need for the test. Now however it's time to focus on the subset.

You should develop your test taking strategy as you take practice exams. Make sure you time yourself to the amount of time the AP exam allows for each section (at least on a couple of practice tests). Your strategy is working if you 1) answer all questions and 2) did not have to guess on otherwise easy problems.

References

  1. https://apstudent.collegeboard.org/apcourse/ap-computer-science-a/course-details "Course Details." AP Computer Science A. N.p., n.d. Web. 29 Mar. 2013.

Author: Nasty Old Dog

2 comments:

  1. I've update the code for this with more examples look at the last blog post in April 2013. Same code
    but more examples coded in.

    ReplyDelete
  2. The link to the latest version is:
    http://nastyolddog.blogspot.com/2013/04/ap-computer-science-study-guide-as.html

    ReplyDelete