0% found this document useful (0 votes)
8 views

Basics of Java

The document provides a comprehensive overview of Java, covering its definition, features, and key concepts such as Object-Oriented Programming principles, exception handling, multithreading, and memory management. It also includes practical programming exercises and examples related to basic Java programming, arrays, strings, and file I/O. Additionally, it discusses advanced topics like generics, interfaces, and access modifiers.

Uploaded by

chaithu kasa11
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Basics of Java

The document provides a comprehensive overview of Java, covering its definition, features, and key concepts such as Object-Oriented Programming principles, exception handling, multithreading, and memory management. It also includes practical programming exercises and examples related to basic Java programming, arrays, strings, and file I/O. Additionally, it discusses advanced topics like generics, interfaces, and access modifiers.

Uploaded by

chaithu kasa11
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Basics of Java

1. What is Java?

o Java is a high-level, class-based, object-oriented programming language that is designed


to have as few implementation dependencies as possible.

2. What are the main features of Java?

o Simple, Object-Oriented, Distributed, Multithreaded, Platform-Independent, Secured,


Robust, Portable, High Performance, Dynamic, and Interpreted.

3. What is the difference between JDK, JRE, and JVM?

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.

4. What is bytecode in Java?

o Bytecode is the compiled format for Java programs. Once the code is compiled, it turns
into bytecode, which the JVM interprets and runs.

5. Explain the difference between == and equals() method in Java.

o == checks for reference equality (whether they are the same object). equals() method
checks for value equality (whether they are meaningfully equivalent).

Object-Oriented Programming Concepts

6. What are the four principles of Object-Oriented Programming?

o Encapsulation, Inheritance, Polymorphism, Abstraction.

7. What is encapsulation in Java?

o Encapsulation is the wrapping of data (variables) and methods (functions) together as a


single unit. It also involves restricting access to certain components (using access
modifiers).

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.

10. What is abstraction?


o Abstraction is the concept of hiding the complex implementation details and showing
only the necessary features of the object.

Java Basics

11. What is a constructor in Java?

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.

13. Can we overload constructors in Java?

o Yes, Java supports constructor overloading. Multiple constructors can be defined in a


class with different parameter lists.

14. What is the this keyword in Java?

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.

15. What is the super keyword in Java?

o The super keyword is a reference variable that is used to refer to the immediate parent
class object.

Exception Handling

16. What is an exception in Java?

o An exception is an event that disrupts the normal flow of the program. It is an object
which is thrown at runtime.

17. What is the difference between checked and unchecked exceptions?

o Checked exceptions are checked at compile-time, whereas unchecked exceptions are


checked at runtime.

18. What are the keywords used for exception handling in Java?

o try, catch, finally, throw, and throws.

19. What is the difference between throw and throws?

o throw is used to explicitly throw an exception, while throws is used to declare an


exception.

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

21. What is a thread in Java?

o A thread is a lightweight process. It is a separate path of execution.

22. How can we create a thread in Java?

o We can create a thread by extending the Thread class or by implementing the Runnable
interface.

23. What is the lifecycle of a thread in Java?

o New, Runnable, Running, Blocked, Terminated.

24. What is synchronization in Java?

o Synchronization is the capability to control the access of multiple threads to shared


resources.

25. What is a deadlock in Java?

o A deadlock is a situation where two or more threads are blocked forever, waiting for
each other.

Strings

26. What is a String in Java?

o A String is a sequence of characters. In Java, strings are objects of the String class.

27. What is the difference between String, StringBuilder, and StringBuffer?

o String is immutable, whereas StringBuilder and StringBuffer are mutable. StringBuilder is


faster but not thread-safe, whereas StringBuffer is thread-safe.

28. How do you compare two strings in Java?

o Using the equals() method for content comparison and == for reference comparison.

29. What is the use of the intern() method in Java?

o The intern() method returns a canonical representation for the string object, which is a
unique string from the string pool.

30. How do you convert a String to an int in Java?

o Using Integer.parseInt() or Integer.valueOf() method.

Memory Management

31. What is the garbage collection in Java?


o Garbage collection is the process of reclaiming the runtime unused memory
automatically.

32. What is the finalize() method in Java?

o The finalize() method is called by the garbage collector before an object is destroyed.

33. What are the types of memory areas allocated by JVM?

o Method Area, Heap, Stack, Program Counter Register, Native Method Stack.

34. What is the difference between Heap and Stack memory?

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.

35. What are memory leaks in Java?

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.

Input and Output

36. What are the types of I/O streams in Java?

o Byte Streams and Character Streams.

37. What is the difference between FileInputStream and FileReader in Java?

o FileInputStream is used for reading raw bytes, whereas FileReader is used for reading
characters.

38. How do you read a file in Java?

o Using classes like FileInputStream, FileReader, BufferedReader, Scanner, etc.

39. What is serialization in Java?

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.

40. What is the transient keyword in Java?

o The transient keyword in Java is used to indicate that a field should not be serialized.

Classes and Objects

41. What is a class in Java?

o A class is a blueprint from which individual objects are created.

42. What is an object in Java?

o An object is an instance of a class. It has state and behavior.


43. What is the difference between an instance variable and a class variable?

o An instance variable is associated with an instance of a class, whereas a class variable


(declared with the static keyword) is associated with the class itself.

44. What is method overloading in Java?

o Method overloading is when multiple methods in the same class have the same name
but different parameters.

45. What is method overriding in Java?

o Method overriding is when a subclass provides a specific implementation for a method


that is already defined in its superclass.

Keywords

46. What is the static keyword in Java?

o The static keyword is used for memory management mainly. It can be used with
variables, methods, blocks, and nested classes.

47. What is the final keyword in Java?

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.

48. What is the abstract keyword in Java?

o The abstract keyword is used to declare a class that cannot be instantiated or a method
that must be implemented by a subclass.

49. What is the synchronized keyword in Java?

o The synchronized keyword is used to control the access of multiple threads to shared
resources.

50. What is the volatile keyword in Java?

o The volatile keyword is used to indicate that a variable's value will be modified by
different threads.

Interfaces and Abstract Classes

51. What is an interface in Java?

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.

52. What is an abstract class in Java?


o An abstract class is a class that cannot be instantiated and is declared with the abstract
keyword. It can have abstract methods (without implementation) and concrete methods
(with implementation).

53. What is the difference between an interface and an abstract class?

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.

54. Can we create an instance of an abstract class in Java?

o No, we cannot create an instance of an abstract class.

55. Can an interface extend another interface in Java?

o Yes, an interface can extend another interface.

Packages and Access Modifiers

56. What are packages in Java?

o Packages are namespaces that organize classes and interfaces by functionality.

57. What are the access modifiers in Java?

o The access modifiers in Java are public, protected, default (no modifier), and private.

58. What is the public access modifier?

o The public access modifier indicates that the member is accessible from any other class.

59. What is the protected access modifier?

o The protected access modifier indicates that the member is accessible within its own
package and by subclasses.

60. What is the private access modifier?

o The private access modifier indicates that the member is accessible only within its own
class.

Advanced Concepts

61. What is a nested class in Java?

o A nested class is a class defined within another class.

62. What is a static nested class in Java?

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.

64. What is an anonymous inner class in Java?

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.

65. What is the instanceof keyword in Java?

o The instanceof keyword is used to test whether an object is an instance of a specific class
or implements an interface.

Generics

66. What are generics in Java?

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.

67. What is type erasure in Java?

o Type erasure is the process by which generic type information is removed at runtime and
replaced with appropriate casts to ensure type safety.

68. What are bounded type parameters in generics?

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.

69. What is a wildcard in generics?

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>).

70. What is the difference between List<Object> and List<?> in Java?

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.

Basic Java Programming

1. Write a Java program to reverse a string without using the StringBuilder or StringBuffer
classes.

2. Write a Java program to check if a given number is a prime number.

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.

8. Write a Java program to find the second largest element in an array.

9. Write a Java program to remove duplicate elements from an array.

10. Write a Java program to find the sum of elements in an array.

Strings

11. Write a Java program to check if a given string is a palindrome.

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

21. Write a Java program to handle ArrayIndexOutOfBoundsException.

22. Write a Java program to handle NullPointerException.

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.

25. Write a Java program to use finally block to close resources.


File I/O

26. Write a Java program to read a file and display its content line by line.

27. Write a Java program to write data to a file.

28. Write a Java program to copy content from one file to another.

29. Write a Java program to count the number of words in a file.

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.

34. Write a Java program to create a thread pool using ExecutorService.

35. Write a Java program to demonstrate inter-thread communication using wait() and notify().

You might also like