Skip to content

About

Refreshing Core Java Knowledge and learning about modern Java changes and features

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

180 Commits

Folders and files

Repository files navigation

Everything about Boolean and boolean in Java

Click to reveal.

Is boolean a primitive type or an object wrapper?

Click to reveal answer.
  A primitive data type.

Is Boolean a primitive type or an object wrapper?

Click to reveal answer.
  An object wrapper class.

Where is a local boolean variable typically stored in memory?

Click to reveal answer.
 On the **stack**. If its a local variable inside a method. In the heap if its a class field
image

Where is a Boolean object stored in memory?

Click to reveal answer.
 On the heap.

What is the default value of a boolean class field?

Click to reveal answer.
false

What is the default value of a Boolean class field?

Click to reveal answer.
null

Can a primitive boolean store a null value?

Click to reveal answer.
No.

Why is Boolean used instead of boolean in Java Collections?

Click to reveal answer.
Java Generics do not support primitive data types.

What is the term for automatically converting a boolean to a Boolean?

Click to reveal answer.
Autoboxing.

What is the term for automatically converting a Boolean to a boolean?

Click to reveal answer.
Unboxing.

Which method does the compiler inject during autoboxing of a boolean?

Click to reveal answer.
Boolean.valueOf()

Which method does the compiler inject during unboxing of a Boolean?

Click to reveal answer.
booleanValue()

What happens at runtime when Java attempts to unbox a null Boolean?

Click to reveal answer.
A NullPointerException is thrown.

Why is boolean preferred over Boolean for high-performance loops?

Click to reveal answer.
To avoid the memory overhead and GC pressure caused by creating wrapper objects.

What is the null-safe way to check if a Boolean variable is true?

Click to reveal answer.
Boolean.TRUE.equals(variable)

How many bits of information does a boolean logically represent?

Click to reveal answer.
1 bit.

Why does Boolean have more memory overhead than boolean?

Click to reveal answer.
It includes an object header in addition to the primitive value.

What is the short-circuit behavior of the && operator?

Click to reveal answer.
It skips the right-hand side if the left-hand side is false.

What is the short-circuit behavior of the || operator?

Click to reveal answer.
It skips the right-hand side if the left-hand side is true.

How does the & operator differ from && when used with booleans?

Click to reveal answer.
& evaluates both sides regardless of the first value (no short-circuiting).

How does the | operator differ from || when used with booleans?

Click to reveal answer.
| evaluates both sides regardless of the first value (no short-circuiting).

What is the result of true ^ true in Java?

Click to reveal answer.
false

Which operator returns true only if the two boolean inputs are different?

Click to reveal answer.
The XOR operator (^).

What is the result of the Logical NOT (!) operator applied to true?

Click to reveal answer.
false

Why should .equals() be used instead of == to compare two Boolean objects?

Click to reveal answer.
== compares memory references, while .equals() compares logical values.

What is the fundamental difference between boolean and Boolean in Java?

Click to reveal answer.
boolean is a primitive data type, while Boolean is an object wrapper class.

What is the default value of a boolean variable when declared as a class field?

Click to reveal answer.
false

What is the default value of a Boolean object reference when declared as a class field?

Click to reveal answer.
null

Why does a Boolean object have higher memory overhead than a boolean primitive?

Click to reveal answer.
It is stored on the heap and includes an object header plus the internal primitive value.

Why can't you store boolean primitives directly in a List or Map?

Click to reveal answer.
Java Generics do not support primitives; they require **object types** like Boolean.

What is the result of attempting to call a method like .toString() on a boolean primitive?

Click to reveal answer.
A **compilation error** (primitives do not have methods).

How does the Java compiler perform autoboxing for a boolean?

Click to reveal answer.
It automatically converts the primitive boolean to a Boolean object using Boolean.valueOf().

How does the Java compiler perform unboxing for a Boolean?

Click to reveal answer.
It extracts the primitive value by automatically calling the booleanValue() method.

What is the "Hidden Danger" of unboxing a Boolean object that is null?

Click to reveal answer.
It throws a NullPointerException (NPE) because the compiler tries to call .booleanValue() on a null reference.

Why is using Boolean in a massive loop for mathematical/logical operations inefficient?

Click to reveal answer.
It causes performance degradation due to constant object creation and increased Garbage Collection pressure.

Why is it dangerous to rely on == when comparing wrapper objects like Integer or Boolean?

Click to reveal answer.
The == operator compares memory references (addresses) rather than logical values.

How can you safely check if a Boolean object is true without risking a NullPointerException?

Click to reveal answer.
Use Boolean.TRUE.equals(yourObject).

How does the short-circuit AND operator (&&) behave if the left operand is false?

Click to reveal answer.
It skips evaluation of the right operand because the overall result is guaranteed to be false.

How does the short-circuit OR operator (||) behave if the left operand is true?

Click to reveal answer.
It skips evaluation of the right operand because the overall result is guaranteed to be true.

In what specific scenario is a short-circuit AND (&&) commonly used to prevent crashes?

Click to reveal answer.
To perform a null check before accessing a member: if (user != null && user.isActive()).

What is the difference between the logical AND (&&) and the bitwise AND (&) when used on booleans?

Click to reveal answer.
&& is short-circuiting, while & forces evaluation of both sides regardless of the first result.

What is the logical result of the Exclusive OR (^) operator?

Click to reveal answer.
It returns true only if the two inputs are different (one true, one false).

What is the result of the expression true ^ true in Java?

Click to reveal answer.
false

What does the logical NOT (!) operator do to a boolean value?

Click to reveal answer.
It flips the value (e.g., !true becomes false).

Java Access Modifiers and Non-Access Modifiers

Click to reveal.

What is an object header in Java?

Click to reveal answer.
An object header in Java is a small block of metadata stored at the beginning of every object in heap memory. It serves as the runtime identity card for the object, allowing the Java Virtual Machine (JVM) to perform memory management, garbage collection, and synchronization.

In the context of Java memory, where is a static field stored?

Click to reveal answer.
In the Metaspace (part of the class metadata), rather than within individual heap objects.

How many copies of a static field exist if a class has 1,000 instantiated objects?

Click to reveal answer.
Exactly one copy, shared by all instances.

What is the primary limitation of a static method regarding instance members?

Click to reveal answer.
It cannot access instance fields or instance methods because it lacks a this parameter.

Why can a static field be accessed even if no objects of that class have been created?

Click to reveal answer.
Because static members belong to the class itself and are initialized when the class is loaded.

What is the effect of the final keyword on a variable?

Click to reveal answer.
It ensures the variable can be assigned only once; its value becomes a constant.
image

When must a final instance field be initialized?

Click to reveal answer.
By the end of every constructor execution.
image

How does marking a method as final affect the inheritance hierarchy?

Click to reveal answer.
It prevents any subclass from overriding that specific method.

What is the architectural impact of declaring a class as final?

Click to reveal answer.
The class cannot be extended (subclassed), ensuring its behavior cannot be altered through inheritance.

Which class in Java is final to ensure its immutability and security cannot be compromised?

Click to reveal answer.
The java.lang.String class.

In a final class, are the instance fields automatically final?

Click to reveal answer.
No; only the methods are effectively final. The fields must be explicitly marked final to be constant.

What is the scope of a member with default (no modifier) access?

Click to reveal answer.
It is accessible only by classes within the same package.

What is the specific visibility of a protected member?

Click to reveal answer.
It is accessible within the same package and by subclasses in different packages.
image

Why is it a best practice to keep instance fields private?

Click to reveal answer.
To maintain encapsulation, allowing internal data representation to change without breaking external code.

What is the role of the java.lang.Object class in Java?

Click to reveal answer.
It is the ultimate ancestor (root) of all classes in the Java hierarchy.

What is required of a concrete subclass that extends an abstract class?

Click to reveal answer.
It must provide implementations for all inherited abstract methods.
image

How does a sealed class differ from a standard class?

Click to reveal answer.
A sealed class explicitly defines which specific subclasses are permitted to extend it.
image image

What is the purpose of the non-sealed keyword?

Click to reveal answer.
It allows a permitted subclass of a sealed class to be further extended by any other class.
image image image image image

What is the difference between extends and implements?

Click to reveal answer.
extends is used for class inheritance (IS-A); implements is used to conform to an interface's contract.

What does the volatile keyword guarantee in a multi-threaded environment?

Click to reveal answer.
It ensures that changes to a variable are visible to all threads immediately by preventing local caching.

What is the primary function of the synchronized keyword on a method?

Click to reveal answer.
It ensures mutual exclusion by requiring a thread to acquire the object's intrinsic lock before execution.

How do you prevent a specific field from being included during object serialization?

Click to reveal answer.
By marking the field with the transient keyword.

What does the native keyword indicate about a method's implementation?

Click to reveal answer.
The method is implemented in a platform-dependent language like C or C++ and is defined outside the JVM.
image

Why can native methods like System.setOut modify final fields?

Click to reveal answer.
Native methods can bypass standard Java access control and language-level safety mechanisms. Native methods can modify final fields because they bypass the  compile-time and runtime access checks by operating directly on raw memory. When you call a native method like System.setOut, the operation is executed via native code (usually C or C++). At this native layer, the concepts of Java language modifiers like final or private do not exist.

What is the function of static import?

Click to reveal answer.
It allows access to static members (fields/methods) without qualifying them with their class name.
image

In terms of OOP design, why would you make a method final?

Click to reveal answer.
To lock its semantics and ensure subclasses cannot change its fundamental behavior.

What is the core definition of encapsulation in OOP?

Click to reveal answer.
Bundling data (fields) and the methods that operate on that data into a single unit (class).

What Java mechanism is primarily used to implement encapsulation?

Click to reveal answer.
What Java mechanism is primarily used to implement encapsulation?

In the context of encapsulation, what is "Data Hiding"?

Click to reveal answer.
Declaring fields as private to prevent direct external access or modification.

How does encapsulation provide controlled access to private fields?

Click to reveal answer.
Through public getter and setter methods.

How does encapsulation enable data validation during assignment?

Click to reveal answer.
Logic inside a setter method can check for invalid inputs before updating the field.

How does encapsulation improve maintainability when changing internal logic?

Click to reveal answer.
It allows internal implementation changes without breaking external code that relies on the public API.

In an encapsulated BankAccount class, why is the balance field private?

Click to reveal answer.
To ensure the balance is only modified through validated methods like deposit() or withdraw().

Which access modifier is used to hide a field so it is accessible only within its own class?

Click to reveal answer.
private.

What is the primary benefit of making a field private and providing only a getter (no setter)?

Click to reveal answer.
It makes the field read-only to external classes.

Why does encapsulation enhance "Data Security"?

Click to reveal answer.
It prevents unauthorized or accidental corruption of an object's internal state.

What is a static block in Java?

Click to reveal answer.
A block of code marked with the static keyword that initializes class-level members.

What is a static block in Java?

Click to reveal answer.
A block of code marked with the static keyword that initializes class-level members.
image image

When exactly does a Java static block execute?

Click to reveal answer.
Automatically, when the class is first loaded into memory by the JVM.

How many times is a static block executed during a class's lifecycle?

Click to reveal answer.
Exactly once.

In what order do a static block and the main method execute?

Click to reveal answer.
The static block executes first.

If a class contains multiple static blocks, in what order are they executed?

Click to reveal answer.
Top-to-bottom, in the exact order they appear in the source code.

Why can a static block NOT access instance variables (non-static fields)?

Click to reveal answer.
Because it executes at class loading, before any object instances (and their this/super contexts) exist.

What is a major advantage of using a static block for field initialization over inline assignment?

Click to reveal answer.
Static blocks allow the use of logic (e.g., loops, conditions) and error handling (try-catch).

Which executes first: a static block or the class constructor?

Click to reveal answer.
The static block (runs at class load, whereas the constructor runs at instantiation).

Where is System.loadLibrary() typically placed to ensure native code is available before class use?

Click to reveal answer.
Inside a static block.

In the initialization sequence of a new object, what is the execution order of the static block, instance initializer, and constructor?

Click to reveal answer.
1. Static block
2. Instance initializer
3. Constructor

Why are static blocks useful for environment setup?

Click to reveal answer.
They allow loading properties or configurations before the application logic or main starts.

Java Virtual Machine (JVM)

Click to reveal.

What tool is used to compile human-readable .java source code into bytecode?

Click to reveal answer.
The javac compiler.

What is the file extension of a file containing Java bytecode?

Click to reveal answer.
.class

What is the primary architectural benefit of Java's bytecode format?

Click to reveal answer.
It is architecture-neutral (portable), allowing the same code to run on any system with a JVM.

Which command-line tool is used to launch the Java Virtual Machine (JVM)?

Click to reveal answer.
The java command.

Which JVM component is responsible for locating and loading .class files into memory?

Click to reveal answer.
Class loaders.

What is the purpose of the Bytecode Verifier during the loading process?

Click to reveal answer.
It ensures the bytecode is safe to run and does not bypass security rules or corrupt memory.

How does a Java Interpreter execute bytecode?

Click to reveal answer.
It translates and executes bytecode one instruction at a time.

What is the main trade-off of using an Interpreter versus a JIT compiler?

Click to reveal answer.
The Interpreter starts the program quickly but is slower for repeated tasks.

In the context of the JVM, what is a hot spot?

Click to reveal answer.
A section of code (like a loop or method) that is executed frequently.

What is the primary function of the Just-in-Time (JIT) Compiler?

Click to reveal answer.
It translates frequently executed bytecode into native machine code for the specific CPU.

What is the goal of Tiered Compilation in modern JVMs?

Click to reveal answer.
To balance startup time and long-term performance using multiple compilers (C1 and C2).

In Tiered Compilation, what is the specific role of the C1 compiler?

Click to reveal answer.
It provides quick initial compilation with simple optimizations.

In Tiered Compilation, what is the specific role of the C2 compiler?

Click to reveal answer.
It performs aggressive, high-level optimizations on the most critical sections of code.

Why is Java generally considered more secure than C or C++ regarding memory?

Click to reveal answer.
The JVM uses a verifier to prevent memory corruption and provides automatic garbage collection.

Java Method Basics

Click to reveal.

Does the Java programming language ever use "pass by reference"?

Click to reveal answer.
No, Java always uses "pass by value".
image image

When passing a primitive (e.g., int, double) to a method, what is received by the parameter?

Click to reveal answer.
A copy of the actual numeric or boolean value.

When passing an object to a method, what exactly is copied and passed?

Click to reveal answer.
A copy of the object reference (the memory address).

When passing an object to a method, what exactly is copied and passed?

Click to reveal answer.
A copy of the object reference (the memory address).

Why can a method modify the internal state (fields) of a passed object?

Click to reveal answer.
Because the parameter is a copy of the reference pointing to the same underlying object in memory.

Can a method reassign a caller's original variable to point to a new object?

Click to reveal answer.
No.

What is the effect on the caller's variables after executing a swap(Object a, Object b) method that reassigns its parameters locally?

Click to reveal answer.
The caller's variables remain unchanged.

Why does a swap(a, b) method fail to swap the caller's original object references?

Click to reveal answer.
The method only swaps local copies of the references.

If a method triples an int parameter x (e.g., x = x * 3), what happens to the original variable in the calling method?

Click to reveal answer.
It remains unchanged.

True or False: In Java, "pass by value" means the method receives a copy of the entire object.

Click to reveal answer.
False; it receives a copy of the reference to the object.

How does the behavior of passing a primitive differ from passing an object reference in Java?

Click to reveal answer.
Both pass a copy, but the object reference copy allows access to the original object's state.

Java Nested Class

Click to reveal.

What is the defining characteristic of a nested class in Java?

Click to reveal answer.
A class that is defined inside another enclosing class.

Can a nested class access private members of its outer enclosing class?

Click to reveal answer.
Yes.

Does an enclosing outer class automatically have direct access to private members of its nested class?

Click to reveal answer.
No (it requires an instance reference of the nested class).

Which access modifiers can be applied to a nested class declared inside an outer class?

Click to reveal answer.
public, protected, private, or package-private (default).

What are the two main categories of nested classes in Java?

Click to reveal answer.
Static nested classes and inner classes (non-static nested classes).

What is the syntax to instantiate a static nested class named StaticNested inside OuterClass?

Click to reveal answer.
OuterClass.StaticNested obj = new OuterClass.StaticNested();

What is the syntax to instantiate a non-static inner class named Inner using an existing outer instance outerObj?

Click to reveal answer.
OuterClass.Inner obj = outerObj.new Inner();

Why can a static nested class NOT directly access instance variables of its outer class?

Click to reveal answer.
Because it is associated with the outer class itself, not with any specific outer instance.

How can a static nested class access a non-static member of its outer class?

Click to reveal answer.
By creating or receiving an explicit object instance of the outer class.

Which outer class members can a non-static inner class directly access?

Click to reveal answer.
Both static and non-static members (including private ones).
image

What are two specialized types of non-static inner classes in Java?

Click to reveal answer.
Local inner classes and anonymous inner classes.

Why can an object of a non-static inner class NOT exist without an instance of the outer class?

Click to reveal answer.
Because every inner class instance holds an implicit strong reference to an outer class instance.

Does an instance of a static nested class hold an implicit reference to an instance of its outer class?

Click to reveal answer.
No.

If Inner is a static nested class, what is the syntax to call its static method msg() from an external class?

Click to reveal answer.
Outer.Inner.msg();

Java Concurrency in Practice

One of the challenges of developing concurrent programs in Java is the mismatch between the concurrency features offered by the platform and how developers need to think about concurrency in their programs.

Threads are the easiest way to tap the computing power of multiprocessor systems. As processor counts increase exploiting concurrency effectively will only become more important.

About

Refreshing Core Java Knowledge and learning about modern Java changes and features

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages