Methods, Classes, and Objects
Questions and Answers 100% Pass
What keyword is used to call a method on an object?
✔✔ The dot (`.`) operator.
How do you create an object from a class?
✔✔ `ClassName obj = new ClassName();`
What is the purpose of the `return` statement in a method?
✔✔ It sends back a value to the method caller.
How do you call a method named `display` inside the same class?
✔✔ `display();`
What keyword is used to create a class in Java?
✔✔ `class`
1
,How do you pass parameters to a method?
✔✔ By defining them inside the parentheses of the method, like `public void greet(String name)
{ }`
How do you return an integer from a method?
✔✔ By specifying `int` as the return type and using `return` inside the method.
What is the default constructor in Java?
✔✔ A constructor with no parameters that initializes an object.
What is an instance variable?
✔✔ A variable defined in a class that belongs to an object.
How do you create a method that accepts two integers and returns their sum?
✔✔ `public int add(int a, int b) { return a + b; }`
What is a constructor?
✔✔ A special method that is called when an object is created.
2
, What is a method in Java?
✔✔ A method is a block of code that performs a specific task and can be called when needed.
How do you define a method in Java?
✔✔ By using a return type, method name, and parentheses, like `public void myMethod() { }`
How do you define a constructor in Java?
✔✔ By using the class name as the method name without a return type.
What keyword is used to refer to the current object in Java?
✔✔ `this`
How do you access a class variable inside an instance method?
✔✔ Using `this.variableName`
Can a method return an object?
✔✔ Yes, a method can return an object of any class.
3