Advanced Computer Science 1: Semester 1 Study Guide

This guide summarizes the major topics and concepts from Units 1-4 of the CodeHS Nitro course. Use this to review before the exam!


Unit 1: Primitive Types

Key Concepts:

  • Data Types:

    • int - Whole numbers (e.g., 5, -10)

    • double - Decimal numbers (e.g., 3.14, -0.5)

    • boolean - True or false values (true, false)

    • String - A sequence of characters (e.g., "Hello", "CodeHS")

  • Variables:

    • Must start with a letter, _, or $ (e.g., myVar, _value).

    • Cannot start with numbers or use reserved keywords like class.

  • Type Casting:

    • Example: Convert int to double:

      int x = 5;  
      double y = (double) x; // y is now 5.0  
      

Unit 2: Using Objects

Key Concepts:

  • Creating Strings:

    • Example:

      String greeting = "Hello";  
      
  • Common String Methods:

    • .length() - Returns the number of characters in the string.

      String word = "CodeHS";  
      System.out.println(word.length()); // Output: 6  
      
    • .toUpperCase() - Converts to uppercase.

      System.out.println("hello".toUpperCase()); // Output: HELLO  
      
    • .charAt(index) - Returns the character at a specific index.

      String s = "Java";  
      System.out.println(s.charAt(0)); // Output: J  
      

Unit 3: Boolean Expressions and if Statements

Key Concepts:

  • Comparison Operators:

    • == - Equal to

    • != - Not equal to

    • > - Greater than

    • < - Less than

    • >= - Greater than or equal to

    • <= - Less than or equal to

  • Logical Operators:

    • && - AND

    • || - OR

    • ! - NOT

  • if Statement Syntax:

    if (condition) {  
        // Code to execute if the condition is true  
    } else {  
        // Code to execute if the condition is false  
    }  
    

Example:

int x = 10;  
if (x > 5) {  
    System.out.println("Greater than 5");  
} else {  
    System.out.println("Less than or equal to 5");  
}  

Unit 4: Iteration

Key Concepts:

for Loop:
  • Use when you know how many times to repeat the loop.

    for (int i = 0; i < 5; i++) {  
        System.out.println(i);  
    }  
    // Output: 0 1 2 3 4  
    
while Loop:
  • Use when the number of repetitions depends on a condition.

    int count = 0;  
    while (count < 3) {  
        System.out.println(count);  
        count++;  
    }  
    // Output: 0 1 2  
    
Infinite Loops:
  • Be careful not to create loops that never stop!

    while (true) {  
        System.out.println("This will run forever!");  
    }  
    

Tips for the Exam

  1. Understand Examples: Practice similar problems to those provided in this guide.

  2. Memorize Key Syntax: Know how to write if statements, for loops, and method calls.

  3. Debugging: Make sure you understand common errors, like mismatched braces or incorrect variable types.

  4. Review Practice Questions: Try solving previous exercises from CodeHS.


Practice Problems

  1. What is the output of the following code?

    int a = 8;  
    double b = 3.0;  
    System.out.println(a / b);  
    

    a. 2
    b. 2.0
    c. 2.6666666666666665
    d. Error

  2. What is the result of true && false?
    a. true
    b. false
    c. Error
    d. 0

  3. Write a for loop to print numbers 1 through 5.


Study well, and good luck on the exam! 😊