Flashcard Deck · 22 cards · Public

Stanford CS106A - Programming Methodologies (Java)

Master Java fundamentals and object-oriented programming with this comprehensive flashcard deck, mirroring Stanford CS106A's renowned curriculum. Dive into core syntax, control structures, OOP principles, recursion, and debugging to build robust problem-solving skills.

Cards in this deck

(22 cards)

Preview terms and definitions before starting your study session.

#1
Term
What is the basic structure of a simple Java program?
Definition
A Java program typically consists of at least one class definition. The program execution begins in the main method, which has the signature public static void main(String[] args).
java6 lines
public class MyProgram {
    public static void main(String[] args) {
        // Program logic goes here
        System.out.println("Hello, CS106A!");
    }
}
#2
Term
Name four primitive data types in Java and their typical uses.
Definition
1. int: Stores whole numbers (e.g., int age = 30;).
2. double: Stores floating-point numbers (e.g., double price = 19.99;).
3. boolean: Stores true or false values (e.g., boolean isActive = true;).
4. char: Stores a single character (e.g., char grade = 'A';).
#3
Term
List the five basic arithmetic operators in Java and their functions.
Definition
1. + (Addition)
2. - (Subtraction)
3. * (Multiplication)
4. / (Division)
5. % (Modulo - remainder after division)
Example: int result = 10 / 3; (result is 3); int remainder = 10 % 3; (remainder is 1).
#4
Term
Explain the purpose and basic syntax of an if/else statement in Java.
Definition
An if/else statement allows a program to execute different blocks of code based on a boolean condition.
java8 lines
int score = 85;
if (score >= 90) {
    System.out.println("Grade: A");
} else if (score >= 80) {
    System.out.println("Grade: B");
} else {
    System.out.println("Grade: C or lower");
}
#5
Term
When would you use a for loop, and what are its three main components?
Definition
A for loop is used for definite iteration, when you know in advance how many times you want to repeat a block of code. Its components are:
1. Initialization: Executed once at the beginning (e.g., int i = 0;).
2. Condition: Evaluated before each iteration; loop continues as long as it's true (e.g., i < 10;).
3. Update: Executed after each iteration (e.g., i++).
java3 lines
for (int i = 0; i < 5; i++) {
    System.out.println("Iteration " + i);
}
#6
Term
When is a while loop more appropriate than a for loop? Provide an example.
Definition
A while loop is used for indefinite iteration, when the number of repetitions is not known beforehand and depends on a condition.
java12 lines
import acm.program.*; // Common in CS106A for console input
public class GuessingGame extends ConsoleProgram {
    public void run() {
        int secret = 7;
        int guess = readInt("Guess a number: ");
        while (guess != secret) {
            println("Incorrect. Try again.");
            guess = readInt("Guess a number: ");
        }
        println("Correct!");
    }
}
#7
Term
What is a method in Java, and what are its key components?
Definition
A method (or function) is a block of code that performs a specific task. Key components include:
  • Access Modifier: (e.g., public, private)
  • Static/Instance: (static or not)
  • Return Type: The type of value the method returns (void if none)
  • Method Name: (e.g., calculateSum)
  • Parameters: Input values the method accepts (e.g., (int a, int b))
java3 lines
public int add(int num1, int num2) {
    return num1 + num2;
}
#8
Term
Differentiate between a class and an object in Java.
Definition
  • A class is a blueprint or a template for creating objects. It defines the properties (fields) and behaviors (methods) that objects of that class will have.
  • An object is an instance of a class. It's a concrete entity created from the class blueprint, occupying memory and having specific values for its properties.
Example: Car is a class, myCar = new Car(); creates an object myCar.
Showing 8 of 22 cards in this deck.
Study All Now

Related Flashcard Sets

Explore similar study materials created by the community

Browse community decks