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:

  1. 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)

  2. Which of the following is not a primitive data type in Java?

    a) int b) double c) string d) boolean

  3. In Java, which keyword is used to create an instance of a class?

    a) new b) this c) class d) instance

  4. What is the keyword used to define a subclass in Java?

    a) superclass b) sub c) extends d) implements

  5. 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:

  1. 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.

  2. Create an instance of the "Rectangle" class from question 6 and print its area.

  3. Write a Java program that takes an integer as input and prints whether it is even or odd.

  4. 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.

  5. Create an object of the "Person" class from question 9, set its name and age, and then print both values.

Theory Questions:

  1. Explain the concept of encapsulation in Java and why it is important in object-oriented programming.

  2. What is method overloading in Java? Provide an example.

  3. What is the difference between "public," "private," "protected," and "default" (package-private) access specifiers in Java?

  4. Describe the difference between "static" and "instance" variables and methods in Java.

  5. 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

Popular posts from this blog

FrontEnd - FAQs - Part 1

Java Script - FAQs

CoreJava - ClassesObjectsMethods - Assignment