All Flashcards
What is a method?
A block of code that performs a specific task.
What is a void method?
A method that performs an action but does not return a value.
What is a non-void method?
A method that performs an action and returns a value.
What is a static method?
A method that belongs to the class itself, not to any specific object.
What is a non-static method?
A method that operates on a specific object.
What is a method signature?
The method name and the parameter list (types and names of inputs).
Define 'parameter'.
Input values that a method can accept to modify its behavior.
What does 'dot notation' refer to?
The way to call methods using the class/object name, a dot, and the method name.
What is the purpose of methods?
To organize code into reusable blocks, making programs more modular and easier to manage.
What is the behavior of a method?
The actions an object can perform.
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 are the differences between void and non-void methods?
Void: Performs actions, no return value. Non-void: Performs actions, returns a value.
What are the differences between static and non-static methods?
Static: Belongs to the class, called using class name. Non-static: Belongs to the object, called using object name.
Compare calling static vs. non-static methods.
Static: ClassName.methodName(). Non-static: objectName.methodName(). Static methods don't require an object instance.
What are the differences between parameters and arguments?
Parameters: Variables in method definition. Arguments: Actual values passed when calling the method.
Compare public, private, and protected access modifiers.
Public: Accessible from anywhere. Private: Accessible only within the class. Protected: Accessible within the class, subclass, and package.
Compare System.out.print() and System.out.println().
print(): Prints to the console without a newline. println(): Prints to the console with a newline.
Compare if and switch statements.
if: Evaluates boolean expressions. switch: Evaluates a single variable against multiple constant values.
Compare for and while loops.
for: Used when the number of iterations is known. while: Used when the number of iterations is unknown.
Compare classes and objects.
Classes: Blueprint for creating objects. Objects: Instances of a class.
Compare local and instance variables.
Local: Declared inside a method, scope limited to that method. Instance: Declared inside a class but outside any method, belongs to an object.