zuai-logo

All Flashcards

Identify the error: ```java public class Example { public void myMethod(int a) { System.out.println(a); } public static void main(String[] args) { myMethod(5); } } ```
`myMethod` is not static and cannot be called directly from the static `main` method. An object of the `Example` class must be created first.
What is the output? ```java public class Test { public static void printMessage() { System.out.println("Hello"); } public static void main(String[] args) { Test.printMessage(); } } ```
Hello
Complete the code to call the `setName` method on `myDog`: ```java Dog myDog = new Dog(); // Complete the line below __________; ```
myDog.setName("Buddy");
Identify the error: ```java public class MathUtils { public int add(int a, int b) { return a + b; } public static void main(String[] args) { System.out.println(add(5, 3)); } } ```
The `add` method is not static and cannot be called directly from the static `main` method without creating an object of the `MathUtils` class.
What is the output? ```java public class Counter { public int count = 0; public void increment() { count++; } public static void main(String[] args) { Counter c = new Counter(); c.increment(); System.out.println(c.count); } } ```
1
Complete the code to call the static method `incrementMinimumWage` from the class `Salary`: ```java // Complete the line below __________; ```
Salary.incrementMinimumWage();
Identify the error: ```java public class Printer { public void print(String message) { System.out.println(message); } public static void main(String[] args) { String msg = "Hello, world!"; Printer.print(msg); } } ```
The `print` method is not static and cannot be called directly using the class name. An object of the `Printer` class must be created first.
What is the output? ```java public class Greeter { public void greet(String name) { System.out.println("Hello, " + name + "!"); } public static void main(String[] args) { Greeter g = new Greeter(); g.greet("Alice"); } } ```
Hello, Alice!
Complete the following code to create an object of type `Car` called `myCar` and then call the method `setMake` with the argument `"Toyota"`. ```java public class Car { String make; public void setMake(String newMake) { make = newMake; } public static void main(String[] args) { // Complete the code below } } ```
```java Car myCar = new Car(); myCar.setMake("Toyota"); ```
What is the output? ```java public class Calculator { public int square(int num) { return num * num; } public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(calc.square(5)); } } ```
25
What is the significance of the `void` keyword in a method header?
It indicates that the method does not return a value.
How do you call a static method?
Use the class name and dot notation: `ClassName.methodName();`
How do you call a non-static method?
Use the object name and dot notation: `objectName.methodName();`
Why is the method signature important?
It's crucial for the compiler to identify and call the correct method.
What happens when a method is called?
The program jumps to the method's code, executes it, and returns to the point where it was called.
What are methods essential for?
Organizing code into reusable blocks and making programs more modular.
What is the role of parameters in methods?
They allow methods to accept input values, modifying their behavior.
What does a method signature consist of?
The method name and the parameter list.
What is the purpose of static methods?
They belong to the class and act as general tools for any object of the class.
What is the purpose of non-static methods?
They operate on a specific object, with each object having its own version.
What are the general steps to call a method in Java?
1. Create an object of the class (if the method is non-static). 2. Use dot notation: `objectName.methodName(parameters);` or `ClassName.methodName(parameters);` for static methods.
What are the steps to define a method?
1. Declare the method signature (access modifier, return type, method name, parameters). 2. Write the method body (the code that performs the task).
What are the steps to call a void method?
1. Create an object of the class (if non-static). 2. Use dot notation: `objectName.methodName(parameters);` or `ClassName.methodName(parameters);` for static methods. No return value is assigned.
What are the steps to call a non-void method?
1. Create an object of the class (if non-static). 2. Use dot notation: `objectName.methodName(parameters);` or `ClassName.methodName(parameters);` for static methods. 3. Store or use the return value.
What are the steps to create a class in Java?
1. Declare the class using the `class` keyword. 2. Define instance variables (attributes). 3. Define methods (behaviors). 4. Optionally, define constructors to initialize objects.
What are the steps to compile and run a Java program?
1. Write the Java code in a `.java` file. 2. Compile the code using `javac YourFile.java`. 3. Run the compiled code using `java YourFile`.
What are the steps to create an object in Java?
1. Declare a variable of the class type. 2. Use the `new` keyword followed by the class constructor to create a new object. 3. Assign the new object to the variable.
What are the steps to declare a variable?
1. Specify the data type. 2. Provide a name for the variable. 3. Optionally, initialize the variable with a value. Example: `int age = 25;`
What are the steps to use an if statement?
1. Write the `if` keyword followed by a boolean expression in parentheses. 2. Write the code to be executed if the expression is true within curly braces. 3. Optionally, add `else` or `else if` blocks for alternative conditions.
What are the steps to use a for loop?
1. Write the `for` keyword followed by initialization, condition, and increment/decrement statements in parentheses. 2. Write the code to be executed in each iteration within curly braces.