Java Assertions Quiz – 20 Questions with Answers
20 questions · 15 min · Medium
Test your knowledge of declarations and access control in Java with 15 questions covering variables, methods, classes, interfaces, and Java access modifiers.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
C. Both A and a data type can be used
Java variables can be declared using an explicit data type, such as int age, and local variables can also use var when type inference is supported.
C. public
A public member can be accessed from classes in the same package as well as classes in other packages, subject to normal Java access rules.
C. private
A private member is accessible only within the top-level class or the class that declares it.
D. Package-private
When no access modifier is specified, the member has package-private access and can be accessed by classes in the same package.
B. Access within the same package and through inheritance in other packages
A protected member is accessible within the same package and also to subclasses in other packages, subject to Java's protected-access rules.
C. public or package-private
A top-level class can be declared public or have no access modifier, which gives it package-private access. Top-level classes cannot be private or protected.
B. int count;
In Java, the data type is written before the variable name, so int count; is a valid declaration.
C. final
The final keyword prevents a variable from being assigned a new value after initialization.
A. public int calculate() { }
A Java method declaration places the access modifier before the return type, followed by the method name and parameter list.
C. private
Private members have the most restricted normal access because they are accessible only within their declaring class.
C. They are directly accessible only within their declaring class
A private field or method cannot be directly accessed from unrelated classes or subclasses.
B. package-private
Package-private access, obtained by omitting an access modifier, allows access from classes in the same package.
B. It is implicitly public and abstract
A traditional interface method declared without a body is implicitly public and abstract. Interfaces can also contain default, static, and private methods with bodies.
C. static
The static keyword declares a class member that belongs to the class rather than to individual objects.
A. To control visibility and access to classes and members
Access modifiers control where classes, fields, methods, and constructors can be accessed, helping enforce encapsulation and organize program structure.