Test1QnA Flashcards

1
Q

System.out.println()

A

Prints a message to the console.

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

Arrays.sort(arr)

A

Sorts an array in ascending order.

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

Collections.sort(list)

A

Sorts a list in ascending order.

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

LocalDate.now()

A

Gets the current date.

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

Files.readAllLines(Path)

A

Reads all lines from a file into a List<String>.</String>

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

Math.abs(x)

A

Returns the absolute value of x.

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

Math.max(a, b)

A

Returns the larger of two numbers.

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

Math.min(a, b)

A

Returns the smaller of two numbers.

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

Math.pow(x, y)

A

Returns x raised to the power of y.

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

Math.sqrt(x)

A

Returns the square root of x.

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

Math.round(x)

A

Rounds x to the nearest integer.

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

Math.random()

A

Returns a random number between 0.0 and 1.0.

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

str.length()

A

Returns the length of the string.

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

str.charAt(index)

A

Returns the character at the given index.

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

str.substring(start, end)

A

Returns a substring from start to end.

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

str.indexOf(sub)

A

Returns the index of the first occurrence of sub.

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

str.equals(anotherString)

A

Compares two strings for equality.

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

str.replace(old, new)

A

Replaces occurrences of old with new.

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

str.toLowerCase()

A

Converts the string to lowercase.

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

str.toUpperCase()

A

Converts the string to uppercase.

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

str.trim()

A

Removes leading and trailing spaces.

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

Character.isLetter(ch)

A

Checks if ch is a letter.

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

Character.isDigit(ch)

A

Checks if ch is a digit.

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

Character.isWhitespace(ch)

A

Checks if ch is a whitespace character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Character.toUpperCase(ch)
Converts ch to uppercase.
26
Character.toLowerCase(ch)
Converts ch to lowercase.
27
Random rand = new Random();
Creates a new Random object.
28
rand.nextInt(bound)
Returns a random integer between 0 (inclusive) and bound (exclusive).
29
rand.nextDouble()
Returns a random double between 0.0 and 1.0.
30
rand.nextBoolean()
Returns a random boolean value (true or false).
31
rand.nextFloat()
Returns a random float between 0.0 and 1.0.
32
What are the fundamental components of a Java program?
A Java program consists of classes, methods, variables, and statements. The main method is the entry point of execution.
33
What are the four pillars of Object-Oriented Programming (OOP) in Java?
The four pillars are Encapsulation, Inheritance, Polymorphism, and Abstraction.
34
What are packages in Java?
Packages are namespaces that organize related classes and interfaces, preventing name conflicts.
35
What are Java’s primitive data types?
Java has 8 primitive types: byte, short, int, long, float, double, char, and boolean.
36
How do you create a string in Java?
Strings can be created using String str = "Hello"; or String str = new String("Hello");.
37
What is the difference between if-else and switch statements?
if-else is used for evaluating boolean expressions, while switch is used for matching values against multiple cases.
38
What is a static method in Java?
A static method belongs to a class rather than an instance and can be called using ClassName.methodName().
39
What is the purpose of the Java API?
The Java API provides built-in classes and methods for common operations like math calculations, string manipulation, and file handling.
40
What method in the Math class returns the square root of a number?
Math.sqrt(x) returns the square root of x.
41
How do you concatenate two strings in Java?
You can use the + operator (str1 + str2) or the concat() method (str1.concat(str2)).
42
How do you generate a random integer in Java?
You can use Random rand = new Random(); int num = rand.nextInt(bound); where bound specifies the range.
43
What is the difference between while and do-while loops?
A while loop checks the condition before execution, while a do-while loop runs at least once before checking the condition.
44
When should you use a for loop instead of a while loop?
Use a for loop when the number of iterations is known in advance; use a while loop when the condition is evaluated dynamically.
45
How can you iterate over a string using a loop?
You can use a for loop: for(int i = 0; i < str.length(); i++) { System.out.println(str.charAt(i)); }
46
Write a Java program that prints "Hello, World!".
java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
47
What is String[] args in the main method, and is it necessary?
String[] args allows command-line arguments to be passed to the program. It is not strictly necessary for program execution, but it must be included in the main method signature.
48
What are the four pillars of OOP in Java?
Encapsulation, Inheritance, Polymorphism, and Abstraction.
49
What is method overloading? Provide an example.
Method overloading allows multiple methods with the same name but different parameters. Example: java class MathOps { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }
50
What is a package in Java?
A package is a namespace that organizes related classes and prevents name conflicts. Example: java package mypackage; public class MyClass { public void display() { System.out.println("Hello!"); } }
51
What will be the output of int i = (int) 10.5; System.out.println(i);?
10 (Explicit type casting from double to int truncates decimal part.)
52
Convert a string "123" into an integer.
java String str = "123"; int num = Integer.parseInt(str);
53
How do you extract "Progr" from "Java Programming"?
java String str = "Java Programming"; System.out.println(str.substring(5, 10));
54
How do you check if a string contains "Java"?
java String sentence = "I love Java"; boolean contains = sentence.contains("Java"); System.out.println(contains); // true
55
Write a program to check if a number is even or odd.
java int num = 10; if (num % 2 == 0) System.out.println("Even"); else System.out.println("Odd");
56
What is the difference between if-else and switch?
if-else works with boolean expressions, while switch is used for matching values against multiple cases.
57
What does Math.sqrt(64) return?
8
58
Find the maximum of two numbers using Math class.
java System.out.println(Math.max(10, 20));
59
How do you generate a random number between 1 and 100?
java Random rand = new Random(); int num = rand.nextInt(100) + 1;
60
What is the output of: for (int i = 1; i <= 5; i++) System.out.print(i + " ");
1 2 3 4 5
61
Convert a for loop into a while loop: for (int i = 1; i <= 5; i++) { System.out.print(i + " "); }
java int i = 1; while (i <= 5) { System.out.print(i + " "); i++; }
62
Write a program to print each character of "Java" using a loop.
java for (int i = 0; i < "Java".length(); i++) { System.out.println("Java".charAt(i)); }
63
Reverse a string using a loop.
java String str = "Hello"; String reversed = ""; for (int i = str.length() - 1; i >= 0; i--) reversed += str.charAt(i); System.out.println(reversed); // olleH