0% found this document useful (0 votes)
4 views8 pages

nd079 Java Myskill Practice Sets

The document outlines the mySkill Assessment for the Udacity nd079 Java Programming Nanodegree, featuring multiple-choice questions covering topics such as OOP concepts, collections, streams, functional programming, JUnit testing, and Maven lifecycle. Each question includes an explanation of the correct answer to enhance understanding. The assessment aims to prepare students for evaluating their knowledge in Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

nd079 Java Myskill Practice Sets

The document outlines the mySkill Assessment for the Udacity nd079 Java Programming Nanodegree, featuring multiple-choice questions covering topics such as OOP concepts, collections, streams, functional programming, JUnit testing, and Maven lifecycle. Each question includes an explanation of the correct answer to enhance understanding. The assessment aims to prepare students for evaluating their knowledge in Java programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Udacity nd079 Java Programming

Nanodegree
mySkill Assessment — Official-Style MCQ Practice Sets

3
Question Sets 9
Total Questions 3
Difficulty Levels

Instructions
• Each question has exactly one correct answer.

• Read all four options carefully before selecting.

• The correct answer with full explanation appears after each question.

• Aim to mentally commit to an answer before reading the explanation.

Udacity nd079 Java Programming Nanodegree — mySkill Assessment Prep Page 1


Topics: Classes · Inheritance · Polymorphism · Access Modifiers · Colle
ntals & OOP

Q1.1 OOP — Overloading vs Overriding Medium

Which of the following statements correctly describes the difference between


method overloading and method overriding in Java?

A Overloading occurs in a subclass; overriding occurs in the same


class.
Overloading means defining multiple methods with the same name but
B different parameter lists in the same class; overriding means a
subclass provides a new implementation of an inherited method with
the same signature.

C Overloading and overriding are identical — the terms are


interchangeable.

D Overriding requires the @Overload annotation; overloading requires


the @Override annotation.

✓ CORRECT ANSWER
B. Overloading means defining multiple methods with the same name but
different parameter lists in the same class; overriding means a subclass
provides a new implementation of an inherited method with the same
signature.
Overloading is resolved at compile-time (static dispatch): same method name,
different parameter types/count within the same class. Overriding is resolved at
runtime (dynamic dispatch): a subclass redefines an inherited method with the
identical signature. @Override is used for overriding, not overloading.

Q1.2 OOP — Abstract vs Interface Medium

A Java class needs to share common state (instance fields) with subclasses AND
also needs to fulfill a contract used by unrelated classes. Which design best
satisfies both requirements?

A Use only an interface, because interfaces can hold instance


fields.

B Use an abstract class for the shared state and have it implement a
separate interface for the contract.

C Use two abstract classes — one for state, one for the contract —
and extend both.

Udacity nd079 Java Programming Nanodegree — mySkill Assessment Prep Page 2


D Declare all fields as static in an interface to share state.

✓ CORRECT ANSWER
B. Use an abstract class for the shared state and have it implement a
separate interface for the contract.
Abstract classes can hold instance fields and constructor logic, making them ideal
for shared state. Interfaces cannot hold instance state (only static final
constants). A class can implement multiple interfaces but only extend one class — so
using an abstract class + interface is the correct combination. Java doesn't support
extending two classes (option C).

Q1.3 Collections — HashMap vs TreeMap Medium

Which statement correctly distinguishes HashMap from TreeMap in Java?

A HashMap maintains insertion order; TreeMap does not.

B HashMap allows null keys; TreeMap does not allow null keys and
keeps keys in natural sorted order.

C TreeMap is faster than HashMap for all operations.

D Both HashMap and TreeMap are synchronized by default.

✓ CORRECT ANSWER
B. HashMap allows null keys; TreeMap does not allow null keys and keeps keys
in natural sorted order.
HashMap uses hashing — O(1) average for get/put, allows one null key, does NOT
guarantee order. TreeMap is backed by a Red-Black tree — O(log n) for get/put, keys
are always sorted (natural order or Comparator), and does NOT allow null keys
(throws NullPointerException). Neither is synchronized by default.

Udacity nd079 Java Programming Nanodegree — mySkill Assessment Prep Page 3


Topics: Lambda · Stream API · Optional · Method References · Function
a: Streams & Functional Programming

Q2.1 Streams — Code Output Medium

What is printed to the console when the following code executes?

List<Integer> nums = [Link](1, 2, 3, 4, 5, 6);

List<Integer> result = [Link]()


.filter(n -> n % 2 == 0) // keep even numbers
.map(n -> n * n) // square each
.collect([Link]());

[Link](result);

A [1, 4, 9, 16, 25, 36]

B [4, 16, 36]

C [2, 4, 6]

D [1, 9, 25]

✓ CORRECT ANSWER
B. [4, 16, 36]
filter(n -> n % 2 == 0) retains only even elements: [2, 4, 6]. map(n -> n * n) then
squares each: 2^2=4, 4^2=16, 6^2=36. Result: [4, 16, 36]. Option A squares all six
numbers. Option C applies filter but skips map. Option D squares the odd numbers [1,
3, 5].

Q2.2 Functional Interfaces Medium

Which functional interface from [Link] best represents a function that


accepts a String and returns an Integer?

A Consumer<String>

B Supplier<Integer>

C Function<String, Integer>

D Predicate<String>

Udacity nd079 Java Programming Nanodegree — mySkill Assessment Prep Page 4


✓ CORRECT ANSWER
C. Function<String, Integer>
Function<T,R> represents T -> R. Here T=String, R=Integer, so
Function<String,Integer> is correct. Consumer<T> accepts T but returns void.
Supplier<R> takes nothing and returns R. Predicate<T> accepts T and always returns
boolean. Method signature: R apply(T t).

Q2.3 Optional Hard

Consider the following code. Which option prints 'DEFAULT' without throwing an
exception when name is empty?

Optional<String> name = [Link]();

// Which line prints 'DEFAULT' safely?

A [Link]([Link]());

B [Link]([Link]("DEFAULT"));

C [Link]([Link]());

D [Link]([Link]([Link]::println));

✓ CORRECT ANSWER
B. [Link]([Link]("DEFAULT"));
orElse("DEFAULT") returns the wrapped value if present, otherwise returns the
fallback "DEFAULT" — never throws. [Link]() throws NoSuchElementException when
empty. orElseThrow() also throws when empty. ifPresent() only executes the consumer
if a value is present (does nothing here), and returns void — so it can't be passed
to println.

Udacity nd079 Java Programming Nanodegree — mySkill Assessment Prep Page 5


Topics: Maven Lifecycle · JUnit 5 Annotations · Mockito · Multi-module P
ent: Maven, JUnit 5 & Mockito

Q3.1 JUnit 5 — Unit Test Isolation Hard

A developer is testing calculateDiscount(Customer c). [Link]()


makes a real database call. Which approach correctly isolates the unit under test?

public class PricingService {


public double calculateDiscount(Customer c) {
if ([Link]()) return 0.20;
return 0.05;
}
}

A Annotate the test with @SpringBootTest so the full application


context and real DB are available.

Create a mock with [Link]([Link]), stub


B when([Link]()).thenReturn(true), call calculateDiscount(c),
then assert the result.

C Pass null as the Customer argument and catch NullPointerException


to verify behavior.

D Use @BeforeEach to open a real DB connection and insert a premium


customer row before each test.

✓ CORRECT ANSWER
B. Create a mock with [Link]([Link]), stub
when([Link]()).thenReturn(true), call calculateDiscount(c), then assert
the result.
Mockito isolates PricingService from its dependency. Stubbing
when([Link]()).thenReturn(true) controls the collaborator's behavior without
any real DB call. @SpringBootTest is integration testing (loads full context).
Options C and D either introduce undefined behavior or real I/O — both violate the
unit-test principle of testing one class in isolation.

Q3.2 Maven — Lifecycle Medium

In what order does Maven execute phases when you run 'mvn package'?

A package -> compile -> test

B validate -> compile -> test -> package

Udacity nd079 Java Programming Nanodegree — mySkill Assessment Prep Page 6


C compile -> package -> test -> install

D test -> compile -> validate -> package

✓ CORRECT ANSWER
B. validate -> compile -> test -> package
Maven's default lifecycle is sequential: validate -> initialize -> generate-sources
-> ... -> compile -> test -> package -> ... -> install -> deploy. Running 'mvn
package' triggers all preceding phases up to and including package. So validate,
compile, and test always run before package completes.

Q3.3 Java 9+ Modules Hard

A [Link] contains only 'module [Link] {}' with no exports. What


happens when another module tries to use a public class from [Link]?

A The public class is accessible because it is declared public.

B The compiler throws an error — public types are inaccessible


without an exports directive.

C The class is accessible at runtime but not at compile time.

D The class is accessible only if both modules are on the classpath


(not module path).

✓ CORRECT ANSWER
B. The compiler throws an error — public types are inaccessible without an
exports directive.
Java's module system enforces strong encapsulation. Without an 'exports
[Link]' directive in [Link], the package is module-private — even
public classes inside it are inaccessible to other modules at both compile time and
runtime. The compiler will report 'package is not visible' or 'does not export'.
Adding the classpath bypasses the module system entirely (option D is partially true
but misleads — the question is about the module path scenario).

Udacity nd079 Java Programming Nanodegree — mySkill Assessment Prep Page 7


Good luck with your mySkill Assessment!
Prepared for Udacity nd079 — Java Programming Nanodegree · ATCI Cohort 9

Udacity nd079 Java Programming Nanodegree — mySkill Assessment Prep Page 8

You might also like