Infosys Questions Java

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is the role of a constructor?

A constructor initializes a new object of a class. It sets initial values and performs any setup or initialization.

Abstract classes vs. interfaces

Abstract Classes: Cannot be instantiated on their own and must be inherited by other classes. They can contain a mix of complete and incomplete methods. Interfaces: Cannot contain any implementation code. All methods are abstract by default until Java 8 introduced default methods with implementations.

Describe abstraction and polymorphism?

Abstraction: Hiding the complex reality while exposing only the necessary parts. It's commonly implemented using abstract classes and interfaces. Polymorphism: The ability of an object to take on many forms. Most commonly, it means that a parent class reference can refer to a child class object.

Tell me about access and non-access modifiers?

Access Modifiers: public, private, protected, and default (no modifier) control access levels for classes, variables, methods, and constructors. Non-access Modifiers: include static, final, abstract, synchronized, etc., which provide other functionalities rather than access control.

Array vs ArrayList?

Array: Fixed size, can hold primitive data types and objects. ArrayList: A resizable array implementation of the List interface. Can only hold objects, not primitives.

Tell me about the different kinds of exceptions. What are some examples of checked and unchecked exceptions?

Checked Exceptions: Exceptions that must be either caught or declared in the method signature. Example: IOException, SQLException. Unchecked Exceptions: Exceptions that do not need to be explicitly handled (runtime exceptions). Example: NullPointerException, ArrayIndexOutOfBoundsException.

What is a class and object

Class: A blueprint for creating objects, providing initial values for state (attributes) and implementations of behavior (methods). Object: An instance of a class. When a class is defined, no memory is allocated until an object of that class is created.

How can I remove duplicate elements from a List?

Convert the List to a Set because a Set does not allow duplicates. Then you can convert the Set back to a List if needed.

How do you handle exceptions?

Exceptions are handled using try, catch, and finally blocks. try is used to enclose the code that might throw an exception, catch is used to handle the exception, and finally is a block of code that is always executed.

How do exceptions and errors differ?

Exceptions: Conditions that a program should catch, representing conditions that a reasonable application might want to catch. Errors: Represent abnormal conditions that should not be caught by most applications, usually related to the environment in which the application is running.

What is the garbage collector? Can you force garbage collection?

Garbage collection is the process by which Java programs perform automatic memory management. Java removes objects that are no longer being used to free up resources. While you cannot force garbage collection, you can request it by calling System.gc(), but there is no guarantee it will happen immediately.

What happens if I don't make the main method static?

If main is not static, JVM would require an instance of the class to call main(), which is not feasible for the JVM as there is no way to create an instance before the main() method is called.

What are the different scopes in java?

Instance, or object, scope: Instance scope means that the variable is attached to individual objects created from the class. When an instance-scoped variable is modified, it has no effect on other, distinct objects of the same class. Class, or static, scope: Class scoped variables reside on the class definition itself. This means that when objects update a class-scoped variable, the change is reflected across all instances of the class. Class scope is declared with the static keyword. Method scope: Method scope is the scope of a variable declared within a method block, whether static or instance. Method-scoped variables are only available within the method they are declared; they do not exist after the method finishes execution. Block scope: Block scoped variables only exist within the specific control flow block, of which there are several in Java: for, while, and do-while loops, if/else-if/else blocks, switch cases, or even just regular blocks of code declared via curly braces ({}). After the block ends, variables declared within it are no longer available.

What does default do in an interface method?

It provides a default implementation for a method in an interface. This allows new methods to be added to interfaces without affecting the classes that implement these interfaces.

What is the difference between JDK, JVM, & JRE?

JDK (Java Development Kit): Includes tools needed for writing and running Java programs, like the compiler and debugger. JVM (Java Virtual Machine): An abstract machine that runs Java bytecodes. It provides a runtime environment in which Java bytecode can be executed. JRE (Java Runtime Environment): Includes the JVM and the libraries Java applications need to run.

What is Java Memory? Describe Stack and Heap

Java Memory primarily refers to the allocation and management of memory for Java processes in the JVM. Stack: Stores primitive values specific to a method and references to objects in heap memory. Each thread has its own stack. Heap: A shared memory area where Java objects are stored. It's managed by the garbage collector.

Have knowledge of the Collections API (List, Set, Queue, how they differ from arrays)

List: An ordered collection that can contain duplicate elements. Elements can be accessed by their integer index. Set: A collection that does not allow duplicate elements. It models the mathematical set abstraction. Queue: A collection used to hold multiple elements prior to processing. Elements are typically ordered in FIFO (first-in-first-out) order. Difference from Arrays: Arrays have a fixed size while collections are more flexible and provide more powerful data structures.

HashTable vs HashMap?

Nulls: HashMap allows null values as keys, whereas HashTable does not allow nulls. Order: HashTables guarantee order, whereas HashMaps do not. Synchronicity: HashTable is synchronized, meaning that it is thread-safe and can be shared between multiple threads, whereas HashMap is not synchronized, and cannot be shared without proper synchronization.

What is OOP? Elaborate on the four pillars

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields, and code in the form of procedures. Four Pillars: Encapsulation: Keeping the data (attributes) and the code (methods) inside the same unit or class. Inheritance: Allows a class to inherit properties and methods from another class. Polymorphism: Allows objects to be treated as instances of their parent class, making it easy to use them interchangeably. Abstraction: Hiding complex implementation details and showing only the essential features of the object.

What is an Optional?

Optional is a container object used to contain not-null objects. Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as 'available' or 'not available' instead of checking null values.

Overloading vs Overriding?

Overloading: Method overloading allows a class to have more than one method having the same name, if their parameter lists are different. Overriding: Method overriding, in object-oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses.

What is unit testing? How do we do it in Java?

Unit testing involves testing individual components of the software program or application. In Java, this is typically done using testing frameworks like JUnit. You write test cases and use assertions to make sure the actual results match the expected outcomes.

How to compare (equate) 2 objects in java?

Use the equals() method to compare objects for equality and hashCode() to ensure the hash table performance.

What are Wrapper classes?

Wrapper classes convert primitive data types into objects; objects are needed to modify the data. Examples include Integer for int, Character for char, etc.

Can you implement multiple interfaces in Java? Can you extend multiple classes?

Yes, a class can implement multiple interfaces but can only extend one class due to Java's avoidance of the "Diamond Problem" (multiple inheritance)

Difference between final and finally?

final: A keyword used to restrict the user, which can be applied to class, methods, and variables. A final class cannot be subclassed, a final method cannot be overridden, and a final variable cannot be changed once it is initialized. finally: Used in exception handling to create a block of code that follows a try-catch block. A finally block is executed regardless of whether an exception is thrown or caught.

Explain each of the part of public static void main(String[] args){}

public: Modifier - Means that the main method can be called from any other class. static: Modifier - Indicates that the method can be run without the need to instantiate a class object. void: Return type - Means the method doesn't return any value. main: Method name - The entry point of any Java application. String[] args: Parameter - args stores Java command line arguments and is an array of type java.lang.String.

What does the static keyword do?

The static keyword denotes that a particular member belongs to a type itself, rather than to instances of that type. This means static methods can be called without creating an instance of the class.

What is the JIT compiler?

The Just-In-Time (JIT) compiler is part of the JVM that improves the performance of Java applications by compiling bytecode into native machine code at runtime.

What is a singleton?

The Singleton pattern is a design pattern that restricts the instantiation of a class to a single object. It ensures that there is only one instance of the class throughout the application and provides a global point of access to that instance. Public singletons are accessible from all parts of the program without the need for a reference.


Ensembles d'études connexes

Internet, Intranets, and Extranets

View Set

NURS114: Chapter 2 Collecting Subjective Data

View Set

Biology - Meiosis vs. Mitosis pt.1

View Set