OOP With Java - Final Exam

ENSAI 2A UE8

Author

Ludovic Deneuville

Published

April 24, 2025

Instructions

  • Paper-based exam
  • Duration: 1h30
  • 1 handwritten A4 sheet (front and back) allowed
  • No calculator
ImportantBefore you start

The subject is composed of multiple pages.

  • You can answer in English or French.
  • The exercises can be addressed in the order of your choice.
  • If you get stuck on a question, quickly move on to the next one.
  • Unless otherwise stated, the questions expect short answers.
  • Strictly follow instructions. If unclear, write your assumptions.
  • Respect good coding and language practices.
  • Answer the MCQ by copying the question number and chosen answers; no justification required.

1 Course questions (2 points)

NoteInstructions

Give short answers to the following questions:

  1. What is a constructor?
  2. What commands did you use to compile and run your Java code?
  3. Explain what is brute force?
  4. What is a mutation test?
  5. What is mapReduce and what are its stages?

2 MCQ (10 points)

NoteInstructions

Each question can have one or more correct answers (at least one answer is correct).

Correct answer: 1 point. Partially correct answer: ratio. If there is a wrong answer: 0.

What will happen if you try to use a local variable without initializing it?

  1. It will be assigned a default value of 0.
  2. It will be assigned a default value of null.
  3. It will cause a compilation error.
  4. The compiler will automatically initialize it for you.

What can be said about Java’s type system?

  1. Java uses strong typing, as a variable cannot change its type once declared.
  2. Java uses dynamic typing, like Python.
  3. Java uses static typing, as types are checked at compile-time.
  4. Java allows changing the type of a variable at runtime.

Which characteristics describe Java as a programming language?

  1. Java is portable because it can run on any machine with a JVM installed.
  2. Java is compiled to bytecode, allowing it to be run on different platforms.
  3. Java follows the “Write Once, Run Anywhere” principle.
  4. Java does not have automatic memory management, which must be handled by developers.

What are the valid signatures of a main method in Java?

  1. public void main(String[] args)
  2. public static void main(String[] args)
  3. static public main(String args[])
  4. void main(String[] args)

Using the class below, what code is used to create a Person object?

public class Person {
    private String name;
    private int age;
    private boolean isStudent = true;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
  1. Person p = Person ("Lisa", 30, false);
  2. Person p = Person ("Lisa", 30);
  3. Person p = new Person ("Lisa" , 30);
  4. p = new Person ("Lisa", 30, true);

Which data structure is most suitable for storing the days of the week?

single answer

  1. Enum
  2. Set
  3. ArrayList
  4. Map<Integer, String>

What does lazy evaluation mean in programming?

  1. Evaluating expressions only when their values are needed.
  2. Storing intermediate results to avoid recalculating them.
  3. Always evaluating all expressions at the start of the program.
  4. Automatically optimizing code to make it run faster.

Which file is essential for any Maven project?

  1. build.xml
  2. application.yml
  3. pom.xml
  4. config.xml

Which statements are true about Apache Maven?

  1. It simplifies dependency management by automatically downloading required libraries.
  2. It enforces a standardized project structure.
  3. It is primarily a code editor with built-in compilation features.
  4. It provides a standardized build lifecycle.
  5. It requires manual configuration of every dependency.

Which layer does this MysteryClass belong to?

public class MysteryClass {
    @Autowired
    private AthleteService athleteService;

    @GetMapping("/api/athletes")
    public ResponseEntity<List<Athlete>> getAllAthletes() {
        List<Athlete> athletes = athleteService.findAll();
        return ResponseEntity.ok(athletes);
    }
}
  1. Controller
  2. Repository
  3. Model
  4. Service

Which are key features of the Spring Framework?

  1. Dependency Injection
  2. Inversion of Control
  3. Aspect-Oriented Programming
  4. Garbage Collection

3 Java Basics (4 points)

  1. Write a function isPositive in Java that takes an integer and returns true if positive or zero, false otherwise.

  2. Write a Java function that prints multiplication tables from 1×1 to 9×9 in this format:

1 x 1 = 1
1 x 2 = 2
...
1 x 9 = 9
-----------
2 x 1 = 2
...
9 x 9 = 81
  1. What will the code below display?
int i = 0;
while (i < 10) {
    i++;
    if (i == 2) {
        continue;
    } else if (i == 5) {
        break;
    }
    System.out.println(i);
}
  1. Give a short example of code using a Lombok annotation and explain its usefulness.

4 Java Code (4 points)

public class Fraction {
    private int numerator;
    private int denominator;

    public Fraction(int numerator, int denominator) {
        if (denominator == 0) {
            throw new IllegalArgumentException("Denominator cannot be zero.");
        }
        this.numerator = numerator;
        this.denominator = denominator;
    }
}
  1. Can the code below work outside the Fraction class? If not, suggest a solution.
Fraction f = new Fraction(1, 2);
System.out.println(f.denominator);
  1. Override the toString method so it returns the fraction in the format “numerator/denominator”.

  2. Add a public method inverse that returns a new Fraction with the numerator and denominator swapped. Write JavaDoc.

  3. Write unit tests for inverse using JUnit.

To check exceptions: assertThrows(ExpectedException.class, () -> {<Code>});

  1. Give git commands to push your code to the remote repository.