Java_3 Flashcards

(8 cards)

1
Q

Which of the following statements about the stack and heap are correct?
The stack is used for storing objects and the heap is used for storing method calls
Primitive data types and objects references are stored on the stack
The stack is organised in a LIFO while the heap is organised in FIFO manner
The stack is limited in size; the heap can dynamically allocate memory as needed
Objects an their instance variables are stored on the heap

A

✅ Primitive data types and object references are stored on the stack

✅ The stack is limited in size; the heap can dynamically allocate memory as needed

✅ Objects and their instance variables are stored on the heap

Stack is indeed LIFO (Last In, First Out).
❌ Heap is not FIFO; it’s a large memory area managed by the JVM for dynamic allocation. It has no ordering like FIFO.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Select the correct statements about thread states
thread in a BLOCKED state may be waiting for monitor to enter synchronised block
thread in a DEAD state is in a deadlock
thread in a TIMED_WAITING state may be waiting for the end Thread.sleep() operation
thread in a WAITING state may be waiting for monitor to enter synchronised block

A

✅ Thread in a BLOCKED state may be waiting for monitor to enter synchronized block

✅ Thread in a TIMED_WAITING state may be waiting for the end of a Thread.sleep() operation. Other causes: join(timeout), wait(timeout), LockSupport.parkNanos(), etc.

A thread in TERMINATED (or DEAD) state has completed execution, either by:Returning from the run() method or Being stopped by an unhandled exception
A thread in WAITING is waiting indefinitely for another thread to: notify() / notifyAll() or or complete (e.g., join() without timeout)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why are generics invariant and what it means

A

it means List<SubType> is NOT a subtype of List<SuperType>, even if SubType is a subtype of SuperType.
Because Java's generics ensure type safety at compile time.
If this were allowed:List<Number> numbers = new ArrayList<Integer>();
numbers.add(3.14); // Adding a Double to a list that actually holds Integers!
Solution is to use wildcards where flexibility is needed</Integer></Number></SuperType></SubType>

Covariant (read-only):
List<? extends Number> numbers = new ArrayList<Integer>();
Can read elements (as Number)
Can’t safely add anything (except null)</Integer>

Contravariant (write-only):
List<? super Integer> numbers = new ArrayList<Number>();
Can add Integers
Can’t safely read as a specific type (just as Object)</Number>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is PECS Rule

A

PECS: “Producer Extends, Consumer Super”
Producer ? extends T If a structure produces T values (read)
Consumer ? super T If a structure consumes T values (write)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is type erasure (generics)

A

Java does not retain generic type information at runtime — due to type erasure.

At compile time:
List<? extends Number> list = new ArrayList<Integer>();
The compiler knows you're using a list of some subtype of Number (like Integer).
It allows reading as Number, but not writing, because it can’t guarantee which exact type is safe to add.</Integer>

At runtime:
Due to type erasure, the list becomes just a List (raw type). The actual type argument (Integer) is erased.

Java’s generics are implemented through type erasure to maintain backward compatibility with legacy
Generic type information is only available at compile time
At runtime, generic type arguments are removed (erased)

Generics provide compile-time safety, not runtime introspection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Given the following piece of code, what will be the output:
public static void add5(Integer x) {
x += 5;
}
public static void main (String ars[]) {

    Integer x = 100;
    add5(5);
    System.out.println(x); }
A

100

Java is pass-by-value — for objects, that means “pass-by-value of the reference”.
x inside add5 is a copy of the reference, so reassigning it doesn’t change the original.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what will do following command
git add -u

A

it will stage updated and deleted files
it won’t stage new files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

which of the statements will add 1 to x if x is positive and subtract 1 from x if x is negative, but will leave x as is if x is equal to 0?
if (x>0) x++; else x–;
if (x>0) x++; else if (x<0) x–;
if (x==0) x=0; else x++; x–;
x++; x–;

A

✅ if (x > 0) x++; else if (x < 0) x–;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly