Java - Skill Assignment Quiz

Q1. Given the string "strawberries" saved in a variable called fruit, what would fruit.substring(2, 5) return?

  •  rawb
  •  raw
  •  awb
  •  traw

Q2. How can you achieve runtime polymorphism in Java?

  •  method overloading
  •  method overrunning
  •  method overriding
  •  method calling

Q3. You have an ArrayList of names that you want to sort alphabetically. Which approach would NOT work?

  •  names.sort(Comparator.comparing(String::toString))
  •  Collections.sort(names)
  •  names.sort(List.DESCENDING)
  •  names.stream().sorted((s1, s2) -> s1.compareTo(s2)).collect(Collectors.toList())

Q4. By implementing encapsulation, you cannot directly access the class's _ properties unless you are writing code inside the class itself.

  •  private
  •  protected
  •  no-modifier
  •  public

Q5. Which is the most up-to-date way to instantiate the current date?

  •  new SimpleDateFormat("yyyy-MM-dd").format(new Date())
  •  new Date(System.currentTimeMillis())
  •  LocalDate.now()
  •  Calendar.getInstance().getTime()

Q6. The runtime system starts your program by calling which function first?

  •  print
  •  iterative
  •  hello
  •  main

Q7. Which statement is NOT true?

  •  An anonymous class may specify an abstract base class as its base type.
  •  An anonymous class does not require a zero-argument constructor.
  •  An anonymous class may specify an interface as its base type.
  •  An anonymous class may specify both an abstract class and interface as base types.

 

Q8. Which are valid keywords in a Java module descriptor (module-info.java)?

  •  provides, employs
  •  imports, exports
  •  consumes, supplies
  •  requires, exports

Q9. Which type of variable keeps a constant value once it is assigned?

  •  non-static
  •  static
  •  final
  •  private

Q10. How does the keyword volatile affect how a variable is handled?

  •  It will be read by only one thread at a time.
  •  It will be stored on the hard drive.
  •  It will never be cached by the CPU.
  •  It will be preferentially garbage collected.

Q11. Normally, to access a static member of a class such as Math.PI, you would need to specify the class "Math". What would be the best way to allow you to use simply "PI" in your code?

  •  Add a static import.
  •  Declare local copies of the constant in your code.
  •  This cannot be done. You must always qualify references to static members with the class form which they came from.
  •  Put the static members in an interface and inherit from that interface.

Q12. Which keyword lets you use an interface?

  •  extends
  •  implements
  •  inherits
  •  import

Q13. Why are ArrayLists better than arrays?

  •  You don't have to decide the size of an ArrayList when you first make it.
  •  You can put more items into an ArrayList than into an array.
  •  ArrayLists can hold more kinds of objects than arrays.
  •  You don't have to decide the type of an ArrayList when you first make it.

Q14. Which choice is a disadvantage of inheritance?

  •  Overridden methods of the parent class cannot be reused.
  •  Responsibilities are not evenly distributed between parent and child classes.
  •  Classes related by inheritance are tightly coupled to each other.
  •  The internal state of the parent class is accessible to its children.

Q15. What is a valid use of the hashCode() method?

  •  encrypting user passwords
  •  deciding if two instances of a class are equal
  •  enabling HashMap to find matches faster
  •  moving objects from a List to a HashMap

Q16. What kind of relationship does "extends" denote?

  •  uses-a
  •  is-a
  •  has-a
  •  was-a

Q17. How do you force an object to be garbage collected?

  •  Set object to null and call Runtime.gc()
  •  Set object to null and call System.gc()
  •  Set object to null and call Runtime.getRuntime().runFinalization()
  •  There is no way to force an object to be garbage collected

Q18. Java programmers commonly use design patterns. Some examples are the _, which helps create instances of a class, the _, which ensures that only one instance of a class can be created; and the _, which allows for a group of algorithms to be interchangeable.

  •  static factory method; singleton; strategy pattern
  •  strategy pattern; static factory method; singleton
  •  creation pattern; singleton; prototype pattern
  •  singleton; strategy pattern; static factory method

Q19. Using Java's Reflection API, you can use _ to get the name of a class and _ to retrieve an array of its methods.

  •  this.getClass().getSimpleName(); this.getClass().getDeclaredMethods()
  •  this.getName(); this.getMethods()
  •  Reflection.getName(this); Reflection.getMethods(this)
  •  Reflection.getClass(this).getName(); Reflection.getClass(this).getMethods()

Q20. What type of variable can be assigned to only once?

  •  private
  •  non-static
  •  final
  •  static

Q21. Which operator is used to concatenate Strings in Java

  •  +
  •  &
  •  .
  •  -

Q22. What phrase indicates that a function receives a copy of each argument passed to it rather than a reference to the objects themselves?

  •  pass by reference
  •  pass by occurrence
  •  pass by value
  •  API call

Q23. In Java, what is the scope of a method's argument or parameter?

  •  inside the method
  •  both inside and outside the method
  •  neither inside nor outside the method
  •  outside the method

Q24. Which characteristic does not apply to instances of java.util.HashSet?

  •  uses hashcode of objects when inserted
  •  contains unordred elements
  •  contains unique elements
  •  contains sorted elements

Q25. Which class does not implement the java.util.Collection interface?

  •  java.util.Vector
  •  java.util.ArrayList
  •  java.util.HashSet
  •  java.util.HashMap

Q26. Which language feature ensures that objects implementing the AutoCloseable interface are closed when it completes?

  •  try-catch-finally
  •  try-finally-close
  •  try-with-resources
  •  try-catch-close

Q27. Which statement about constructors is not true?

  •  A class can have multiple constructors with a different parameter list.
  •  You can call another constructor with this or super.
  •  A constructor does not define a return value.
  •  Every class must explicitly define a constructor without parameters.

Q28. Which code checks whether the characters in two Strings,named time and money, are the same?

  •  if(time <> money){}
  •  if(time.equals(money)){}
  •  if(time == money){}
  •  if(time = money){}

Q29. An _ is a serious issue thrown by the JVM that the JVM is unlikely to recover from. An _ is an unexpected event that an application may be able to deal with in order to continue execution.

  •  exception,assertion
  •  AbnormalException, AccidentalException
  •  error, exception
  •  exception, error

Q30. What keyword is added to a method declaration to ensure that two threads do not simultaneously execute it on the same object instance?

  •  native
  •  volatile
  •  synchronized
  •  lock

Q31. From which class do all other classes implicitly extend?

  •  Object
  •  Main
  •  Java
  •  Class

Q32. Which keyword would you add to make this method the entry point of the program?

  •  exception
  •  args
  •  static
  •  String

Q33. Which data structure would you choose to associate the amount of rainfall with each month?

  •  Vector
  •  LinkedList
  •  Map
  •  Queue

Q34. When you pass an object reference as an argument to a method call what gets passed?

  •  a reference to a copy
  •  a copy of the reference
  •  the object itself
  •  the original reference

Q35. Which choice demonstrates a valid way to create a reference to a static function of another class?

  •  Function<Integer, Integer> funcReference = MyClass::myFunction;
  •  Function<Integer, Integer> funcReference = MyClass.myFunction;
  •  Function<Integer, Integer> funcReference = MyClass().myFunction();
  •  Function<Integer, Integer> funcReference = MyClass::myFunction();

Q36. What is true about a final class?

  •  class declared final is a final class.
  •  Final classes are created so the methods implemented by that class cannot be overriddden.
  •  It can't be inherited.
  •  All of the above.

Q37. Which method can be used to find the highest value of x and y?

  •  Math.largest(x,y)
  •  Math.maxNum(x,y)
  •  Math.max(x,y)
  •  Math.maximum(x,y)

Q38. void accept(T t) is method of -?

  •  Consumer
  •  Producer
  •  Both
  •  None

Q39. Which of these does Stream filter() operates on?

  •  Predicate
  •  Interface
  •  Class
  •  Methods

Q40. Which command will run a FrogSounds app that someone emailed to you as a jar?

  •  jar FrogSounds.java
  •  javac FrogSounds.exe
  •  jar cf FrogSounds.jar
  •  java -jar FrogSounds.jar

READY TO GET STARTED?

Are you ready

Let’s Make Something Amazing Together

Need help? Contact our experts
Tell us about your project