Java 2 Flashcards

1
Q

Q: What is JDK?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: What is JRE?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: What is JVM?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: Why is the Java platform independent?

A

A: Java is platform independent because its bytecode can run on any system with a compatible JVM, regardless of the underlying operating system.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: How many primitive data types are there in Java?

A

A: There are 8 primitive data types in Java: byte, short, int, long, float, double, boolean, and char.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: What is the difference between primitive data types and non-primitive data types?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: What is the difference between == and equals()?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: How do we convert primitive variables to another primitive type?

A

A: By using the typecasting operator.
double myDouble = 3.14159;
int myInt = (int) myDouble;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: What is the Scanner class?

A

A: The Scanner class is used for reading user input and processing it in Java applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: How do you read user input from the console using Scanner?

A

Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: What is String Pool?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: Can you list some common methods of the String class?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q: What is the purpose of the “if” statement in Java?

A

A: The if statement is used to execute a block of code if a specified condition is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Q: Explain the role of the “else if” statement in Java.

A

A: The else if statement is used to test multiple conditions in sequence, executing the code associated with the first true condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Q: Write a Java code snippet to check if a number is positive, negative, or zero using if, else if, and else.

A

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.”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q: What is the purpose of the else statement?

A

A: The else statement is used when none of the preceding conditions in an if-else if-else structure are true, acting as the final fallback.

17
Q

Q: How does the for loop work in Java?

A

A: A for loop allows you to repeatedly execute a block of code as long as a specified condition is true. It consists of initialization, condition, and update steps.
for (int i = 1; i <= 3; i++) {
System.out.println(“Iteration “ + i);
}

18
Q

Q: How does the while loop work?

A

A: The while loop repeatedly executes a block of code as long as a specified condition is true. It checks the condition before each iteration.
int count = 1;
while (count <= 5) {
System.out.println(“Iteration “ + count);
count++;
}

19
Q

Q: How does the do-while loop work?

A

A: The do-while loop guarantees that the code block is executed at least once, checking the condition after each iteration.
int count = 1;
do {
System.out.println(“Iteration “ + count);
count++;
} while (count <= 5);

20
Q

Q: What is an array in Java, and what advantages does it offer over using primitive variables?

A

A: An array is a data structure that allows you to store a collection of elements of the same type under a single variable name. It offers advantages like storage of multiple values, index-based access, and the ability to store objects and primitive values.

21
Q

Q: Can you give an example of how you declare an array?

A

// Using an array literal
int[] arrayLiteral = {1, 2, 3, 4, 5};

// Using the ‘new’ keyword with array size
int[] newArray = new int[5];
newArray[0] = 1;
newArray[1] = 2;
newArray[2] = 3;
newArray[3] = 4;
newArray[4] = 5;

// Iterating over elements of the array
for (int i = 0; i < newArray.length; i++) {
System.out.println(“Element at index “ + i + “: “ + newArray[i]);
}

22
Q

Q: What are methods that return values?

A

A: Methods that return values perform a specific task and provide a result or value as output using the return statement. They are useful for calculations, data retrieval, or producing results for further processing.

23
Q

Q: What are methods that do not return values (void methods)?

A

A: Void methods perform a task or action without returning any specific value, indicated by the void return type. They are used when a result is not needed for further computation or use.

24
Q

Q: What is a class in Java?

A

A: A class is a blueprint or template for creating objects, defining their structure and behavior. It includes fields, methods, and constructors.

25
Q

Q: What can we create inside a class?

A

A: Inside a class, you can create:

Fields (instance variables)
Methods (functions)
Constructors
Objects (instances of the class)

26
Q

Q: What are static variables and methods in Java?

A

A:

Static variables (class variables) are shared among all instances of a class and can be accessed using the class name. They are declared with the static keyword.
Static methods are associated with the class itself and can be called without creating an instance of the class. They are also declared with the static keyword.

27
Q

Q: Can you provide an explanation of WRAPPER classes, auto-boxing, unboxing, and casting in Java?

A

A:

Wrapper classes encapsulate primitive data types as objects, providing utility methods.
Auto-boxing automatically converts primitive types to their corresponding wrapper objects.
Unboxing converts wrapper objects back to primitive types.
Casting converts one data type to another, either between primitives or objects.

// Autoboxing
List numbers = new ArrayList<>();
numbers.add(42); // int to Integer

// Unboxing
int num = numbers.get(0); // Integer to int

// Primitive casting
double doubleValue = 42.0;
int intValue = (int) doubleValue; // double to int

// Object casting
Object obj = “Hello, Java!”;
String str = (String) obj; // Object to String