Creating and Destroying Objects Quiz

An abstract representation of object-oriented programming concepts, featuring diagrams of static factory methods, Singleton patterns, and memory management techniques.

Creating and Destroying Objects Quiz

Test your knowledge on object creation and destruction in programming with our engaging quiz. This quiz covers a variety of concepts, including static factory methods, Singleton patterns, memory management, and much more.

  • Challenge yourself with 9 questions
  • Multiple choice and checkbox formats
  • Instant feedback on your answers
9 Questions2 MinutesCreated by CreatingCode101
What is (are) the advantage(s) of using static factory methods?
Unlike constructors, they are not required to create a new object each time they’re invoked
Unlike constructors, they can return an object of any subtype of their return type
Unlike classes with public or protected constructors, they can be subclassed
They reduce the verbosity of creating parameterized type instances
What sort of constructors or static factories it’s recommended to write when faced with many constructor parameters? (multiple optional parameters)
Telescoping constructor pattern
Builder Pattern
JavaBeans Pattern
Any pattern
Which one of the following is the subject of “unnecessary objects creation”?
String s = new String("Hello World");
public class Person {

   private final Date birthDate;

   // Other fields, methods, and constructor omitted


   public boolean isBabyBoomer() {

      Calendar gmtCal = Calendar.
getInstance(TimeZone.getTimeZone("GMT"));


      gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);

 

      Date boomStart = gmtCal.getTime();

      gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);

 

      Date boomEnd = gmtCal.getTime();

      return birthDate.compareTo(boomStart) >= 0 &&
birthDate.compareTo(boomEnd) < 0;


   }

}
Boolean b = Boolean.valueOf("true");
Using primitives instead of auto-boxed-primitives
What requirement(s) must satisfy a Singleton candidate?
Controls concurrent access to a shared resource
Access to the resource will be requested from multiple parts of the system
There can be only one object
All of the above
Why we should avoid Singletons and static classes?
May introduce global state, Hide dependencies, Can be extended into a factory.
May introduce global state, Get tightly coupled to multiple other classes, Hide dependencies, Can make unit testing classes in isolation difficult.
May introduce global state, Can make unit testing classes in isolation difficult.
May introduce global state, Can make unit testing classes in isolation difficult, Can be extended into a factory.
How we can enforce noninstantiability of a class?
Making the class abstract
Making the constructor private and throw an error using reflection
Making the class final
All of the above
Consider the following simple stack implementation. Can you correct the "memory leak"?

public class Stack {

       private Object[] elements;

       private int size = 0;

       private static final int DEFAULT_INITIAL_CAPACITY = 16;

       public Stack() {

              elements = new Object[DEFAULT_INITIAL_CAPACITY];

       }

       public void push(Object e) {

              ensureCapacity();

              elements[size++] = e;

       }

       public Object pop() {

              if (size == 0)

                     throw new EmptyStackException();

              Object result = elements[--size];

              return result;

       }

      

       private void ensureCapacity() {

              if (elements.length == size)

                     elements = Arrays.copyOf(elements, 2 * size + 1);

       }

}

Add elements[size] = null; before “Object result = elements[--size];”
Add elements[size] = null; after “Object result = elements[--size];”
Change “elements = Arrays.copyOf(elements, 2 * size + 1)” to “elements = Arrays.copyOf(elements, 2 * size - 1);”
Change DEFAULT_INITIAL_CAPACITY to 100
Why we should avoid finalizers?
We shouldn't avoid using finalizers, instead it’s safe to use them and we should help garbage collector to collect faster
It blocks the database.
It decrease the performance.
It creates many threads.
Consider the following code. What it will be printed?

public enum MySingleton {

    INSTANCE;

    private MySingleton() {

        System.out.print("Boo");

    }

}

public static void main(String[] args) {

    System.out.println(MySingleton.INSTANCE);

}

Boo
INSTANCE
Instance
BooINSTANCE
BooInstance
{"name":"Creating and Destroying Objects Quiz", "url":"https://www.quiz-maker.com/QPREVIEW","txt":"Test your knowledge on object creation and destruction in programming with our engaging quiz. This quiz covers a variety of concepts, including static factory methods, Singleton patterns, memory management, and much more.Challenge yourself with 9 questionsMultiple choice and checkbox formatsInstant feedback on your answers","img":"https:/images/course3.png"}
Powered by: Quiz Maker