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?

A constructor is a special method in a class that is called when an object of that class is instantiated.

  1. What commands did you use to compile and run your Java code?
  • javac File.java: Compiles the File.java source file into bytecode.
  • java MyClass: Runs the compiled MyClass bytecode.
  • mvn compile: Compiles the source code of a Maven project.
  • mvn exec:java: Executes the main class of a Maven project.
  1. Explain what is brute force?

Brute force is a problem-solving method that involves systematically checking all possible solutions until the correct one is found.

  1. What is a mutation test?

A mutation test evaluates the effectiveness of your tests by introducing small changes (mutations) to your code and checking if your tests can detect these changes.

  1. What is mapReduce and what are its stages?

MapReduce is a programming model for processing large datasets in a distributed computing environment. It breaks down complex tasks into simpler, parallel operations. The MapReduce workflow consists of three main stages:

  • Split: Input data is divided into smaller chunks.
  • Map: Each chunk is processed by a Map function.
  • Reduce: Values are aggregated by a Reduce function.

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<String>
  3. ArrayList<String>
  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.
public static boolean isPositive(int number) {
    return number >= 0;
}
  1. 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
public static void printMultiplicationTables() {
    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= 9; j++) {
            System.out.println(i + " x " + j + " = " + (i * j));
        }
        if (i < 9) {
            System.out.println("-----------");
        }
    }
}
  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
3
4
  1. Give a short example of code using a Lombok annotation and explain its usefulness.
@AllArgsConstructor
public class Person {

    @Getter
    private String name;
    private int age;
    private boolean isStudent = true;
}

With @AllArgsConstructor, Lombok generates this constructor for you, reducing boilerplate code.

With annotation @Getter, Lombok generates this getter method for you, making the code cleaner and more concise.

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);

The code provided will not work outside the Fraction class because the denominator attribute is private and cannot be accessed directly. To solve this, you can add a public getter method to the Fraction class to access the denominator.

  1. Override the toString method so it returns the fraction in the format “numerator/denominator”.
    @Override
    public String toString() {
        return this.numerator + "/" + this.denominator;
    }
  1. Add a public method inverse that returns a new Fraction with the numerator and denominator swapped. Write JavaDoc.
    /**
     * Returns a new Fraction that is the inverse of this fraction,
     * with the numerator and denominator swapped.
     *
     * @return a new Fraction with the numerator and denominator swapped
     * @throws ArithmeticException if the numerator is zero
     */
    public Fraction inverse() {
        if (this.numerator == 0) {
            throw new ArithmeticException("Numerator cannot be zero for inverse operation.");
        }
        return new Fraction(this.denominator, this.numerator);
    }
  1. Write unit tests for inverse using JUnit.

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

    @Test
    public void testInverseOk() {
        Fraction f1 = new Fraction(1, 2);
        Fraction inverseF1 = f1.inverse();
        assertEquals("2/1", inverseF1.toString());

    @Test
    public void testInverseWithZeroNumerator() {
        Fraction f = new Fraction(0, 2);
        assertThrows(ArithmeticException.class, () -> {
            f.inverse();
        });
    }
  1. Give git commands to push your code to the remote repository.
$ git add Fraction.java
$ git add FractionTest.java
$ git commit -m "add inverse method and tu"
$ git push