JAVA_Questions
JAVA_Questions
class Example {
int instanceVar = 5; // Instance scope
static int staticVar = 10; // Static scope
package mypackage;
import java.util.Scanner;
4. Packages
Packages in Java are used to group related classes. They help in
organising code and avoiding name conflicts.
class Car {
String model;
6. StringBuffer Methods
StringBuffer is used for mutable strings (strings that can change). Key
methods are:
import java.io.*;
- `int`: Keyword
- `number`: Identifier
- `=`, `+`, `-`: Operators
- `10`: Literal
import java.io.*;
- Decision Making:
Using `if-else` and `switch`.
- Looping:
Using `for`, `while`, and `do-while` loops.
// Decision making
if (age >= 18) {
System.out.println("You are an adult");
} else {
System.out.println("You are a minor");
}
// Looping
for (int i = 0; i < 5; i++) {
System.out.println(i); // Output: 0 to 4
}
- Overriding:
Subclass provides a specific implementation of a method that is already
defined in its parent class.
Method Overloading Example:
class Calculator {
public int add(int a, int b) {
return a + b;
}
class Animal {
public void sound() {
System.out.println("Animal sound");
}
}