Basics of Java
Basics of Java
1. What is Java?
o JDK (Java Development Kit) is a software development kit used to develop Java
applications. JRE (Java Runtime Environment) provides libraries, Java Virtual Machine
(JVM), and other components to run applications written in Java. JVM is an abstract
machine that enables your computer to run a Java program.
o Bytecode is the compiled format for Java programs. Once the code is compiled, it turns
into bytecode, which the JVM interprets and runs.
o == checks for reference equality (whether they are the same object). equals() method
checks for value equality (whether they are meaningfully equivalent).
8. What is inheritance?
o Inheritance is a mechanism where a new class (child class) inherits the properties and
behavior of another class (parent class).
9. What is polymorphism?
o Polymorphism means "many forms" and it allows one interface to be used for a general
class of actions. The most common use is when a parent class reference is used to refer
to a child class object.
Java Basics
o A constructor is a block of code that initializes a newly created object. It has the same
name as the class and does not have a return type.
12. What is the difference between a default constructor and a parameterized constructor?
o A default constructor does not take any parameters and initializes the object with
default values. A parameterized constructor takes arguments and initializes the object
with given values.
o The this keyword is a reference to the current object. It is used to refer to the instance
variables and methods of the current object.
o The super keyword is a reference variable that is used to refer to the immediate parent
class object.
Exception Handling
o An exception is an event that disrupts the normal flow of the program. It is an object
which is thrown at runtime.
18. What are the keywords used for exception handling in Java?
20. Can we write multiple catch blocks under a single try block?
o Yes, we can write multiple catch blocks under a single try block to handle different types
of exceptions.
Multithreading
o We can create a thread by extending the Thread class or by implementing the Runnable
interface.
o A deadlock is a situation where two or more threads are blocked forever, waiting for
each other.
Strings
o A String is a sequence of characters. In Java, strings are objects of the String class.
o Using the equals() method for content comparison and == for reference comparison.
o The intern() method returns a canonical representation for the string object, which is a
unique string from the string pool.
Memory Management
o The finalize() method is called by the garbage collector before an object is destroyed.
o Method Area, Heap, Stack, Program Counter Register, Native Method Stack.
o Heap memory is used for dynamic memory allocation for Java objects and JRE classes at
runtime, whereas stack memory is used for the execution of a thread.
o Memory leaks occur when an object is no longer needed but the garbage collector fails
to reclaim its memory because there are still references to it.
o FileInputStream is used for reading raw bytes, whereas FileReader is used for reading
characters.
o Serialization is the process of converting an object into a byte stream so that it can be
easily saved to a file or transferred over a network.
o The transient keyword in Java is used to indicate that a field should not be serialized.
o Method overloading is when multiple methods in the same class have the same name
but different parameters.
Keywords
o The static keyword is used for memory management mainly. It can be used with
variables, methods, blocks, and nested classes.
o The final keyword is used to restrict the user. It can be used with variables, methods, and
classes to make them non-changeable, non-overridable, and non-inheritable,
respectively.
o The abstract keyword is used to declare a class that cannot be instantiated or a method
that must be implemented by a subclass.
o The synchronized keyword is used to control the access of multiple threads to shared
resources.
o The volatile keyword is used to indicate that a variable's value will be modified by
different threads.
o An interface is a reference type in Java that can contain only constants, method
signatures, default methods, static methods, and nested types. Interfaces cannot contain
implementation of methods.
o An abstract class can have both abstract and concrete methods, whereas an interface
can only have abstract methods (until Java 8, where default and static methods were
introduced). A class can implement multiple interfaces but can extend only one abstract
class.
o The access modifiers in Java are public, protected, default (no modifier), and private.
o The public access modifier indicates that the member is accessible from any other class.
o The protected access modifier indicates that the member is accessible within its own
package and by subclasses.
o The private access modifier indicates that the member is accessible only within its own
class.
Advanced Concepts
o A static nested class is a static member of the outer class and can be instantiated
without an instance of the outer class.
63. What is an inner class in Java?
o An inner class is a non-static nested class that has access to other members of the
enclosing class.
o An anonymous inner class is a class without a name and is instantiated in place. It is used
to provide a quick implementation of an interface or an abstract class.
o The instanceof keyword is used to test whether an object is an instance of a specific class
or implements an interface.
Generics
o Generics enable types (classes and interfaces) to be parameters when defining classes,
interfaces, and methods. It allows for type safety and reduces the need for casting.
o Type erasure is the process by which generic type information is removed at runtime and
replaced with appropriate casts to ensure type safety.
o Bounded type parameters allow you to specify bounds for the type parameters,
restricting them to a certain type or a subtype of that type.
o A wildcard represents an unknown type and is denoted by the question mark (?). It can
be used with upper bounds (<? extends T>) or lower bounds (<? super T>).
o List<Object> can hold any type of object, whereas List<?> is a list of an unknown type.
List<?> is more flexible and allows for greater type safety.
1. Write a Java program to reverse a string without using the StringBuilder or StringBuffer
classes.
3. Write a Java program to find the factorial of a given number using both iterative and recursive
methods.
4. Write a Java program to find the Fibonacci series up to a specified number using both iterative
and recursive methods.
5. Write a Java program to swap two numbers without using a temporary variable.
Arrays
6. Write a Java program to find the maximum and minimum elements in an array.
7. Write a Java program to sort an array using the bubble sort algorithm.
Strings
12. Write a Java program to count the number of vowels and consonants in a given string.
13. Write a Java program to find the longest word in a given sentence.
14. Write a Java program to find the frequency of each character in a given string.
15. Write a Java program to remove all occurrences of a given character from a string.
Object-Oriented Programming
16. Create a Person class with attributes name, age, and address. Write methods to get and set
these attributes, and a method to display the person's details.
17. Create a Rectangle class with attributes length and width. Write methods to calculate the area
and perimeter of the rectangle.
18. Create a BankAccount class with attributes accountNumber, balance, and ownerName. Write
methods to deposit and withdraw money, and to display the account details.
19. Create an Employee class that inherits from a Person class. Add an attribute salary and a
method to display the employee's details.
20. Create a Shape interface with methods area and perimeter. Implement this interface in Circle
and Rectangle classes.
Exception Handling
23. Write a Java program to create a custom exception called InvalidAgeException and throw it
when the age is less than 18.
24. Write a Java program to handle multiple exceptions in a single try block.
26. Write a Java program to read a file and display its content line by line.
28. Write a Java program to copy content from one file to another.
30. Write a Java program to merge two files into a third file.
Multithreading
31. Write a Java program to create and start a thread by extending the Thread class.
32. Write a Java program to create and start a thread by implementing the Runnable interface.
33. Write a Java program to demonstrate thread synchronization using the synchronized keyword.
35. Write a Java program to demonstrate inter-thread communication using wait() and notify().