Java Intermediate

0% completed

Previous
Next
Quiz
Question 1
Which of the following is the correct syntax to declare a package in a Java file?
A
package com.example;
B
import com.example;
C
public package com.example;
D
package: com.example
Question 2
What is encapsulation in Java?
A
The process of hiding implementation details and exposing only essential features of an object.
B
The ability to inherit properties from a superclass.
C
The method of combining two classes into one.
D
A technique to perform error handling.
Question 3
Examine the following code snippet:
package com.example;

class Employee {
    private String name;
    private int id;
    
    public Employee(String name, int id) {
        this.name = name;
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
}

public class Test {
    public static void main(String[] args) {
        Employee emp = new Employee("John", 101);
        System.out.println("Employee: " + emp.getName());
    }
}
What does this code illustrate about encapsulation?
A
It shows that member variables should be public.
B
It shows that constructors are unnecessary.
C
It demonstrates controlled access to private data through getters and setters.
D
It demonstrates that packages automatically provide encapsulation.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next