0
0
Javaprogramming~20 mins

Why object-oriented programming is used in Java - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
OOP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use encapsulation in OOP?
Which of the following best explains why encapsulation is used in object-oriented programming?
ATo make programs run faster by avoiding method calls
BTo allow direct access to all object variables from anywhere
CTo hide internal details and protect object data from outside interference
DTo write code without using classes or objects
Attempts:
2 left
πŸ’‘ Hint
Think about how encapsulation helps keep data safe inside an object.
❓ Predict Output
intermediate
2:00remaining
Output of inheritance example
What is the output of this Java code?
Java
class Animal {
    void sound() {
        System.out.println("Some sound");
    }
}
class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}
public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
ASome sound
BBark
CCompilation error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Look at which method is called when an Animal reference points to a Dog object.
❓ Predict Output
advanced
2:00remaining
Polymorphism with method overloading
What will this Java program print?
Java
class Calculator {
    int add(int a, int b) {
        return a + b;
    }
    int add(int a, int b, int c) {
        return a + b + c;
    }
}
public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(2, 3));
        System.out.println(calc.add(1, 2, 3));
    }
}
ARuntime error
B
5
5
CCompilation error due to method overloading
D
5
6
Attempts:
2 left
πŸ’‘ Hint
Check how many arguments each add method takes.
🧠 Conceptual
advanced
2:00remaining
Why use abstraction in OOP?
Which statement best describes the purpose of abstraction in object-oriented programming?
ATo show only essential features and hide complex details from the user
BTo allow users to see and modify all internal data of an object
CTo make all methods public so they can be accessed anywhere
DTo write programs without using any classes
Attempts:
2 left
πŸ’‘ Hint
Think about how abstraction simplifies interaction with objects.
🧠 Conceptual
expert
3:00remaining
Main advantage of using OOP in large software projects
What is the main advantage of using object-oriented programming in large software projects?
AIt helps organize code into reusable and manageable pieces called objects
BIt makes programs run faster by avoiding any use of classes
CIt forces all code to be written in a single file for simplicity
DIt removes the need for any testing or debugging
Attempts:
2 left
πŸ’‘ Hint
Think about how OOP helps manage complexity in big programs.