Moksha - Test 2 - Core Java
Java Coding and Theory Test
Coding and theory test paper covering the topics :Java classes, methods, variables, conditional statements, access specifiers, and object-oriented programming (OOP) concepts.
Instructions:
- Answer all the multiple-choice questions by selecting the correct option.
- Write Java code to solve the coding questions.
- You have 120 minutes to complete the test.
Multiple-Choice Questions:
What is the access modifier used to specify that a method or variable is accessible only within the same class?
a) private b) public c) protected d) default (package-private)
Which of the following is not a primitive data type in Java?
a) int b) double c) string d) boolean
In Java, which keyword is used to create an instance of a class?
a) new b) this c) class d) instance
What is the keyword used to define a subclass in Java?
a) superclass b) sub c) extends d) implements
What is the primary purpose of a constructor in Java?
a) To initialize the object's instance variables. b) To define methods for the class. c) To specify access modifiers. d) To create a new object.
Coding Questions:
Write a Java class named "Rectangle" with instance variables "length" and "width." Include a parameterized constructor to initialize these variables. Also, create a method named "calculateArea" that calculates and returns the area of the rectangle.
Create an instance of the "Rectangle" class from question 6 and print its area.
Write a Java program that takes an integer as input and prints whether it is even or odd.
Define a class called "Person" with private instance variables "name" and "age." Provide getter and setter methods for both variables. Ensure that the "age" variable cannot be set to a negative value.
Create an object of the "Person" class from question 9, set its name and age, and then print both values.
Theory Questions:
Explain the concept of encapsulation in Java and why it is important in object-oriented programming.
What is method overloading in Java? Provide an example.
What is the difference between "public," "private," "protected," and "default" (package-private) access specifiers in Java?
Describe the difference between "static" and "instance" variables and methods in Java.
Briefly explain the concept of inheritance in object-oriented programming and how it is implemented in Java.
Scoring:
- Each multiple-choice question is worth 2 points.
- Each coding question is worth 10 points.
- Each theory question is worth 5 points.
Total Points: 100
Comments
Post a Comment