Q & A in Java basics Part-2
20.
What is the "super" keyword in
Java?
21.
How do you implement inheritance in Java?
22.
Explain the concept of interfaces in Java.
An
interface is a reference type like a class but contains only abstract methods
and constant fields. Classes can implement multiple interfaces.
23.
What is an abstract class in Java?
An abstract class is a
class that cannot be instantiated and can contain both abstract and concrete
methods. It serves as a base for other classes.
24.
How do you achieve multiple inheritance in Java?
Java supports multiple inheritance through interfaces.
25.
What is the "default" keyword in interface methods?
26.
How do you handle multi-threading in Java?
You
can handle multi-threading using Java's built-in "Thread" class or by
implementing the "Runnable" interface.
27.
What is synchronization in Java?
31.
What are access modifiers in Java?
Access modifiers
control the visibility of classes, methods, and variables. Java has four access
modifiers: public, private, protected, and default (package-private).
32.
Explain the "final" keyword in Java.
The "final"
keyword is used to make a variable, method, or class constant and unchangeable.
28.
How do you implement a for loop in Java?
29.
What is a while loop in Java?
30.
How do you implement a do-while loop in Java?
A
do-while loop executes the code block at least once, and then it repeatedly
executes the block if the specified condition is true.
31.
What is the "break" statement in
Java?
32.
What is the "continue" statement in
Java?
The
"continue" statement is used to skip the rest of the loop's body for
the current iteration and move to the next iteration.
33.
How do you read user input from the console
in Java?
You
can use the Scanner
class from the java.util
package to read user input.
Here
is the screenshot of output from IntelliJ IDE:
37.What are the
different types of control statements in Java?
Java has three types of
control statements: selection statements (if-else, switch), loop statements
(for, while, do-while), and branching statements (break, continue, return).
39.
How do you sort an array in Java?
You can use the Arrays.sort()
method to sort an array of primitive data types.
Here
is the screenshot of output from IntelliJ IDE:
40.
How do you sort an ArrayList in Java?
You
can use the Collections.sort()
method to sort an ArrayList of objects.
Here is the screenshot of output from IntelliJ IDE:
42.
What is autoboxing and unboxing in Java?
Autoboxing is the
process of automatically converting a primitive type to its corresponding
wrapper class (e.g., int to Integer). Unboxing is the reverse process.
43.
What is the "StringBuilder" class in Java, and how is
it different from "String" for string manipulation?
The
"StringBuilder" class is used for efficient string manipulation, as
it allows dynamic modifications to strings without creating new objects. In
contrast, the "String" class is immutable, and every modification
creates a new string object.
44.
How do you find the length of an array in Java?
You can use the
"length" property of the array to find its length.
Here
is the screenshot of output from IntelliJ IDE:
45.
What is the Java Virtual Machine (JVM)?
The JVM is a virtual
machine that enables Java bytecode to be executed on any platform. It provides
a runtime environment for Java programs.
46.
What is the "throws" keyword in Java?
The "throws"
keyword is used to declare that a method may throw a specific type of checked
exception.
47.
What is the "try-with-resources"
statement in Java?
The
"try-with-resources" statement is used to manage resources that need
to be closed explicitly, such as files or network connections. It automatically
closes the resources at the end of the block.
48.
What is the purpose of the "assert"
statement in Java?
The
"assert" statement is used to check assumptions during the
development phase and can be enabled or disabled at runtime. If an assertion
fails, it throws an "AssertionError."
49.
How do you convert a string to an integer in
Java?
50.
How do you convert an integer to a string in
Java?
You
can use the String.valueOf()
method or simply concatenate the integer
with an empty string to convert it to a string.
Comments
Post a Comment