1.
Difference between compile-time and runtime polymorphism:
- Compile-time polymorphism (Method Overloading): Resolved during compilation. Same method
name with different parameters.
- Runtime polymorphism (Method Overriding): Resolved during runtime. A subclass provides a
specific implementation of a method already defined in its superclass.
2. What is abstraction? How is it achieved in Java?
- Abstraction is the concept of hiding internal implementation and showing only essential features.
- In Java, it is achieved using:
- Abstract classes
- Interfaces
3. What is a thread? Life cycle of a thread:
- A thread is a lightweight subprocess, the smallest unit of processing.
- Life Cycle of a Thread:
1. New - Thread is created.
2. Runnable - Ready to run.
3. Running - Actively executing.
4. Blocked/Waiting - Waiting for a resource or signal.
5. Terminated - Execution completed.
4. What is Encapsulation? Access specifiers:
- Encapsulation is the process of wrapping data and code together as a single unit.
- Access Specifiers in Java:
- Private - Accessible within the class.
- Default - Accessible within the package.
- Protected - Accessible within the package and subclasses.
- Public - Accessible from everywhere.
5. Difference between String and StringBuffer:
- String is immutable; once created, it cannot be changed.
- StringBuffer is mutable; changes can be made without creating a new object.
- StringBuffer is thread-safe, while String is not.
6. Java Program to count vowels and consonants in a String:
public class VowelConsonantCounter {
public static void main(String[] args) {
String str = "Hello World";
int vowels = 0, consonants = 0;
str = [Link]();
for (char c : [Link]()) {
if ([Link](c)) {
if ("aeiou".indexOf(c) != -1)
vowels++;
else
consonants++;
[Link]("Vowels: " + vowels);
[Link]("Consonants: " + consonants);
}
7. What is a wrapper class? Name all wrapper classes:
- Wrapper classes convert primitive data types into objects.
- Wrapper classes in Java:
- byte - Byte
- short - Short
- int - Integer
- long - Long
- float - Float
- double - Double
- char - Character
- boolean - Boolean