Java Flashcards
Q: What is JDK?
A: The JDK (Java Development Kit) is a software package that includes tools required for Java application development, such as the Java compiler, debugger, and other development utilities.
Q: What is JRE?
A: The JRE (Java Runtime Environment) is a software package essential for running Java applications. It includes the JVM, class libraries, and other components needed to execute compiled Java code.
Q: What is JVM?
A: The JVM (Java Virtual Machine) is responsible for executing Java bytecode. It compiles bytecode into machine code that can run on any computer, providing platform independence and portability for Java programs.
Q: Why is the Java platform independent?
A: Java is platform independent because its bytecode can run on any system with a compatible JVM, regardless of the underlying operating system.
Q: How many primitive data types are there in Java?
A: There are 8 primitive data types in Java: byte, short, int, long, float, double, boolean, and char.
Q: What is the difference between primitive data types and non-primitive data types?
A:
Primitive data types have a fixed size, take up less memory, and perform simple operations. Passing them as arguments works with a copy of the value.
Non-primitive data types have variable size, take up more memory, and can store complex structures and methods. Passing them as arguments works with references, affecting the original object.
Q: What is the difference between == and equals()?
A:
== compares object references and primitive values, checking if two references point to the same object.
equals() checks if two objects have the same values.
Q: How do we convert primitive variables to another primitive type?
A: By using the typecasting operator.
double myDouble = 3.14159;
int myInt = (int) myDouble;
Q: What is the Scanner class?
A: The Scanner class is used for reading user input and processing it in Java applications.
Q: How do you read user input from the console using Scanner?
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
Q: What is String Pool?
A: The String pool is a special area in memory where the JVM stores a pool of String objects to conserve memory and optimize string operations.
Q: Can you list some common methods of the String class?
A:
length()
charAt(int index)
concat(String str)
equalsIgnoreCase(String another)
startsWith(String prefix)
endsWith(String suffix)
indexOf(int ch)
lastIndexOf(int ch)
substring(int beginIndex)
substring(int beginIndex, int endIndex)
trim()
toLowerCase()
toUpperCase()
replace(char oldChar, char newChar)
isEmpty()
contains(CharSequence sequence)
valueOf(int value)
Q: What is the purpose of the “if” statement in Java?
A: The if statement is used to execute a block of code if a specified condition is true.
Q: Explain the role of the “else if” statement in Java.
A: The else if statement is used to test multiple conditions in sequence, executing the code associated with the first true condition.
Q: Write a Java code snippet to check if a number is positive, negative, or zero using if, else if, and else.
int number = 5;
if (number > 0) {
System.out.println(“The number is positive.”);
} else if (number < 0) {
System.out.println(“The number is negative.”);
} else {
System.out.println(“The number is zero.”);
}