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

Java Questions

The document provides a comprehensive guide for preparing for a Java Trainer interview at Infosys, featuring 50 commonly asked questions categorized by topics such as Core Java Concepts, Java Collections Framework, Java 8 Features, and Multithreading and Concurrency. Each question includes detailed explanations of key Java concepts, such as OOP principles, exception handling, and stream operations. This resource is designed to enhance both technical knowledge and teaching methodologies for prospective interviewees.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Questions

The document provides a comprehensive guide for preparing for a Java Trainer interview at Infosys, featuring 50 commonly asked questions categorized by topics such as Core Java Concepts, Java Collections Framework, Java 8 Features, and Multithreading and Concurrency. Each question includes detailed explanations of key Java concepts, such as OOP principles, exception handling, and stream operations. This resource is designed to enhance both technical knowledge and teaching methodologies for prospective interviewees.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Preparing for your Java Trainer interview at Infosys requires a thorough understanding of both

technical concepts and effective teaching methodologies. Below is a curated list of 50 commonly
asked interview questions, categorized by topic, along with conversational explanations to help
you grasp the concepts effectively.

1. Core Java Concepts:

●​ Object-Oriented Programming (OOP):​

6.​ What are the four main principles of OOP?​

■​ Answer: OOP is based on four key principles:​

■​ Encapsulation: Bundling data (variables) and methods that


operate on that data into a single unit, typically a class. This hides
the internal state and requires all interactions to occur through an
object's methods.​

■​ Inheritance: Creating new classes based on existing ones,


allowing for code reusability and the creation of a hierarchical
relationship between classes.​

■​ Polymorphism: Allowing entities to be represented in multiple


forms. In Java, this means that a single method can have different
implementations based on the object it acts upon.​

■​ Abstraction: Hiding complex implementation details and showing


only the essential features of an object. This is achieved using
abstract classes and interfaces.​

7.​ Can you explain the difference between an abstract class and an interface?​

■​ Answer: An abstract class can have both abstract methods (without


implementation) and concrete methods (with implementation). It allows for
code sharing among related classes. An interface, on the other hand, can
only have abstract methods (until Java 8, after which default and static
methods are allowed). Interfaces are used to define a contract that
multiple classes can implement, promoting a form of multiple inheritance.​

●​ Java Data Types: 3. What is the difference between primitive data types and objects in
Java?​

6.​ Answer: Primitive data types (like int, char, float) store simple values and are not
objects; they are stored directly in memory for efficiency. Objects, however, are
instances of classes and can represent more complex structures. They are
stored as references, meaning the variable holds the address of the object in
memory.​

7.​ What are wrapper classes in Java?​

■​ Answer: Wrapper classes provide a way to use primitive data types (int,
char, etc.) as objects. Each primitive type has a corresponding wrapper
class in Java (e.g., Integer for int, Character for char). They are useful
when you need an object reference to a primitive value, such as when
working with collections that can only store objects.​

●​ Exception Handling: 5. How does Java handle exceptions?​

6.​ Answer: Java uses a try-catch-finally mechanism to handle exceptions. Code that
might throw an exception is placed in the try block. If an exception occurs, it's
caught in the catch block, where you can handle it appropriately. The finally block
contains code that executes regardless of whether an exception was thrown,
typically used for cleanup operations.​

7.​ What is the difference between checked and unchecked exceptions?​

■​ Answer: Checked exceptions are exceptions that are checked at


compile-time, meaning the compiler requires you to handle them explicitly
(e.g., IOException). Unchecked exceptions are not checked at
compile-time; they occur during runtime, and handling them is optional
(e.g., NullPointerException).​

2. Java Collections Framework:

●​ Collections Overview: 7. What are the main interfaces in the Java Collections
Framework?​

8.​ Answer: The main interfaces include List, Set, and Map. List allows ordered
collection with duplicates, Set is a collection that doesn't allow duplicates, and
Map holds key-value pairs, with unique keys mapping to specific values.​

9.​ How does a HashMap work in Java?​

■​ Answer: HashMap stores data in key-value pairs and uses a hashing


mechanism to compute an index into an array of buckets or slots, from
which the desired value can be found. When collisions occur (two keys
hash to the same index), it uses linked lists or trees to store multiple
entries in a single bucket.​

3. Java 8 Features:

●​ Lambda Expressions and Functional Interfaces: 9. What is a functional interface in


Java 8?​

12.​Answer: A functional interface is an interface that contains exactly one abstract


method. They can have multiple default or static methods but only one abstract
method. Functional interfaces are the basis for lambda expressions in Java.​

13.​How do lambda expressions improve code readability?​

■​ Answer: Lambda expressions provide a clear and concise way to


represent one-method interfaces using an expression. They eliminate the
need for anonymous class implementations, making the code more
readable and expressive.​

●​ Stream API: 11. What are streams in Java 8, and how are they used? - Answer:
Streams represent a sequence of elements supporting sequential and parallel aggregate
operations. They allow for functional-style operations on collections of data, such as
map, filter, and reduce, enabling concise and readable code for processing collections.​

12.​Can you explain the difference between intermediate and terminal operations in
streams?​

■​ Answer: Intermediate operations return a new stream and are lazy,


meaning they are not executed until a terminal operation is invoked.
Examples include filter and map. Terminal operations produce a result or
side-effect and terminate the stream pipeline, such as collect or forEach.​

4. Multithreading and Concurrency:

●​ Thread Management: 13. What is the difference between a process and a thread? -
Answer: A process is an independent program in execution with its own memory space,
while a thread is a lightweight subprocess, the smallest unit of processing. Threads
within the same process share the same memory space but can execute independently.​

14.​How do you create a thread in Java?​

■​ Answer: There are two main ways to create a thread in Java: by


extending the Thread class and overriding its run method, or by
implementing the Runnable interface and passing an instance to a Thread
object. Implementing Runnable is generally preferred as it allows for
better object-oriented design and flexibility.​

●​ Synchronization: 15. What is synchronization, and why is it important in Java? -


Answer: Synchronization is a mechanism that ensures that only one thread can access a
resource at a time, preventing data inconsistency and race conditions. It's crucial in
multi-threaded applications where multiple threads might access shared resources
concurrently.​

You might also like