Java Lecture Questions
Java Lecture Questions
1995.
Java is a high-level, class-based, object-oriented programming language.
1. What is Java?
Java is an object-oriented programming language and platform that allows
developers to create applications that can run on any device that has a Java
Virtual Machine (JVM) installed.
2. What are the features of Java?
Some key features of Java include platform independence, object-oriented
design, strong security features, and a vast ecosystem of libraries and tools.
3. How do you declare a variable in Java?
Variables in Java are declared using the syntax type variableName;, such as
int x; or String name;.
4. What are the data types in Java?
Java has two main categories of data types: primitive types (e.g., int, double,
boolean) and reference types (e.g., classes, interfaces, arrays).
5. What is the difference between "==" and ".equals()" in Java?
"==" checks for reference equality, while ".equals()" checks for value
equality. For example, a == b checks if a and b are the same object, while
a.equals(b) checks if a and b have the same value.
6. How do you create an object in Java?
Objects are created using the new keyword, such as MyClass obj = new
MyClass();.
7. What is inheritance in Java?
Inheritance is a mechanism in Java where one class can inherit the properties
and behavior of another class using the extends keyword.
8. What is polymorphism in Java?
Polymorphism is the ability of an object to take on multiple forms, such as
method overriding or method overloading.
9. What is encapsulation in Java?
Encapsulation is the practice of hiding an object's internal state and behavior
from the outside world, and only exposing a public interface through which
other objects can interact with it.
10. How do you handle exceptions in Java?
Exceptions are handled using try-catch blocks, where code that might throw
an exception is wrapped in a try block, and the corresponding exception is
caught and handled in a catch block.
11. What is the purpose of the "finally" block in Java?
The finally block is used to execute code that needs to run regardless of
whether an exception is thrown or not, such as closing files or releasing
resources.
12. What is the difference between "throw" and "throws" in Java?
throw is used to explicitly throw an exception, while throws is used to declare
that a method might throw a certain exception.
13. How do you create a thread in Java?
Threads can be created by extending the Thread class or implementing the
Runnable interface.
14. What is synchronization in Java?
Synchronization is a mechanism that ensures that only one thread can access
a shared resource at a time, preventing data corruption and other
concurrency issues.
15. What is the purpose of the "static" keyword in Java?
The static keyword is used to denote that a variable or method belongs to a
class, rather than an instance of the class.
16. How do you import packages in Java?
Packages are imported using the import statement, such as import
java.util.ArrayList;.
17. What is the difference between "ArrayList" and "LinkedList" in
Java?
ArrayList is a resizable array-based implementation of the List interface, while
LinkedList is a doubly-linked list implementation.
18. How do you read input from the user in Java?
Input can be read using the Scanner class or BufferedReader class.
19. What is the purpose of the "this" keyword in Java?
The this keyword is used to refer to the current object, and is often used to
disambiguate between instance variables and method parameters.
20. How do you create a constructor in Java?
Constructors are special methods that are used to initialize objects when they
are created, and have the same name as the class. They are declared without
a return type, such as public MyClass() { }.