What is import java.util.Scanner/Random?
This programming style is based on the concept of “objects” that contain data and methods.
What is object-oriented programming?
This is the process of finding and fixing problems in a Java program.
What is debugging?
public static void main(String[] args) {
System.out.printl("Hello, world!");
}
System.out.println("Hello, world!");
the n is missing
public static void main(String[] args) {
int result = add(5, 3);
System.out.println(result);
}
public static void add(int a, int b) {
return a + b;
}
This statement is used to display text in the console.
What is System.out.println?
These are values sent into a method to customize its behavior.
What are parameters?
This Java feature lets the user choose between options, like a menu.
What is a switch statement?
public static void main(String[] args) {
int name = "John";
System.out.println(name);
}
String name = "John";
int should be String
switch (choice) {
case 1:
System.out.println("You chose 1");
case 2:
System.out.println("You chose 2");
default:
System.out.println("Default choice");
}
missing break; for each case
These are containers for storing data values such as numbers or text.
What are variables?
This type of operator combines boolean expressions, like &&, ||, and !.
What are boolean operators?
This kind of loop is designed to iterate through each element in a collection or array.
What is a for-each loop?
public static void main(String[] args) {
Scanner scanner;
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("You are " + age + " years old.");
}
Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
String password = "1234";
if (password == "1234") {
System.out.println("Access granted");
}
}
if (password.equals("1234")) {
This type of statement allows a program to make decisions based on conditions.
What is an if statement?
These are special methods used to create and initialize objects.
What are constructors?
These are events where something goes wrong during the execution of a program, often requiring special handling.
What are exceptions?
public static void main(String[] args) {
int i = 0;
while (i < 5) {
System.out.println("i is: " + i);
}
}
add i ++; at the end of the loop
public static void main(String[] args) {
return;
System.out.println("This will never print.");
}
unreachable code
either remove return; or move it to the end
This operator is used to combine strings or add numbers.
What is the + operator?
This structure holds multiple values in a single variable, using an index.
What is an array?
This data structure organizes data in rows and columns, like a grid.
What is a two-dimensional array?
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
System.out.println(numbers[3]);
}
index 3 does not exist
public class Player {
int health;
public Player() {
health = 100;
}
public void printHealth() {
System.out.println("Health: " + health);
}
}
public class Game {
public static void main(String[] args) {
Player.printHealth();
}
}
In the Game class should have:
Player p = new Player();
p.printHealth();