0% found this document useful (0 votes)
4 views17 pages

Core JAVA

The document contains a series of multiple-choice questions related to core Java concepts, including control structures, arrays, constructors, inheritance, and polymorphism. It also includes exercises that require writing Java programs to demonstrate understanding of these concepts. The questions cover fundamental aspects of Java programming and object-oriented principles.

Uploaded by

chaithu kasa11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views17 pages

Core JAVA

The document contains a series of multiple-choice questions related to core Java concepts, including control structures, arrays, constructors, inheritance, and polymorphism. It also includes exercises that require writing Java programs to demonstrate understanding of these concepts. The questions cover fundamental aspects of Java programming and object-oriented principles.

Uploaded by

chaithu kasa11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Core-JAVA

1.What Java control structure is used to execute a block of code repeatedly as long as a
specified condition is true?
a. if statement
b. for loop
c. switch statement
d. while loop

2.Which keyword is used in Java to immediately exit from a loop without completing the
current iteration?
a. break
b. continue
c. return
d. exit

3.In Java, how do you declare an array of integers with 5 elements?


a. int array[5];
b. array<int> arr = new array<int>[5];
c. int[] arr = new int[5];
d. arr int[5];

4.What is the index of the first element in a Java array?


a. 0
b. 1
c. -1
d. The index is specified by the programmer

5.Which of the following is true about constructors in Java?


a. A class can have multiple constructors with the same parameter list.
b. Constructors can have a return type.
c. Constructors can be inherited by subclasses.
d. Constructors can be declared as static methods.

6.What is the purpose of the this keyword in Java constructors?


a. It refers to the superclass.
b. It calls another constructor in the same class.
c. It refers to the current instance of the class.
d. It is used to define a new class.

7.In Java, what is the principle that allows you to define a blueprint for creating objects?
a. Abstraction
b. Encapsulation
c. Inheritance
d. Polymorphism

8.What is the primary goal of encapsulation in Java?


a. To allow classes to inherit from multiple other classes.
b. To hide the internal details of a class and restrict access to them.
c. To perform type conversion between different data types.
d. To create objects based on a class blueprint.

9.What is the primary purpose of the "if" statement in Java?


a. To execute a block of code repeatedly.
b. To define a new class.
c. To make decisions based on a condition.
d. To create an array.

10.Which of the following Java control structures is used for multi-way decisions based
on different cases?
a. if statement
b. for loop
c. switch statement
d. while loop
11.In Java, what keyword is used to exit the current iteration of a loop and continue
with the next iteration?
a. break
b. continue
c. return
d. exit

12.What is the output of the following code in Java?

java
Copy code
int x = 5;
if (x < 3) {
System.out.println("A");
} else {
System.out.println("B");
}
a. A
b. B
c. 5
d. 3

13.What is the purpose of the for loop in Java?


a. To make decisions based on conditions.
b. To execute a block of code repeatedly while a condition is true.
c. To define a class.
d. To create objects.

14.Which Java control structure is used to execute a block of code repeatedly until a
specified condition is false?
a. if statement
b. for loop
c. switch statement
d. while loop
15.In Java, the do-while loop will always execute its block of code at least how many
times?
a. Once
b. Twice
c. Based on the condition
d. It depends on the loop counter

16.What is the Java control structure that combines the "if" and "else" conditions to
execute one of two blocks of code?
a. for loop
b. switch statement
c. if-else statement
d. while loop

17.In Java, what is the purpose of the break statement in a loop?


a. To exit the program.
b. To skip the rest of the loop's current iteration and continue with the next iteration.
c. To continue the loop from the beginning.
d. To execute a specific method.

18.What is the primary role of the switch statement in Java?


a. To create objects.
b. To define a class.
c. To execute a block of code repeatedly.
d. To perform multi-way conditional branching based on different cases.

19.Answer: d. To perform multi-way conditional branching based on different cases.


In Java, which package should you import to use the Scanner class?
a. java.util
b. java.io
c. java.text
d. java.lang

20.What is the purpose of the Scanner class in Java?


a. To manipulate arrays.
b. To perform mathematical calculations.
c. To read input from the user or a file.
d. To sort arrays.
21.Which method is used to read an integer input from the user using the Scanner class
in Java?
a. nextLine()
b. nextInt()
c. nextDouble()
d. nextBoolean()

22.How do you create an array of integers with 5 elements in Java?


a. int array(5);
b. array<int> arr = new array<int>[5];
c. int[] arr = new int[5];
d. arr int[5];

23.What is the index of the last element in a Java array with a length of 10?
a. 9
b. 10
c. 1
d. 0

24.How can you access the element at index 3 in a Java array named myArray?
a. myArray(3)
b. myArray.index(3)
c. myArray.get(3)
d. myArray[3]

25.What is the purpose of the length property for arrays in Java?


a. To add elements to an array.
b. To access the first element of the array.
c. To determine the number of elements in the array.
d. To sort the array.
26.How do you iterate through the elements of an array in Java using a loop?
a. You cannot iterate through an array.
b. By using a for loop or a while loop.
c. By using a switch statement.
d. By defining a new class.

27.Which statement is used to populate an array in Java?


a. populate
b. fillArray
c. initialize
d. Arrays cannot be populated.

28.What is the key advantage of using arrays in Java?


a. They can store data of different data types in a single array.
b. They can be resized dynamically at runtime.
c. They provide fast and efficient access to elements based on their index.
d. They automatically sort the elements when added.

29.What is the primary purpose of a constructor in Java?


a. To create a new class.
b. To define global variables.
c. To initialize object properties and allocate resources.
d. To execute mathematical calculations.

30.Which of the following is true about constructors in Java?


a. A class can have multiple constructors with the same parameter list.
b. Constructors can have a return type.
c. Constructors can be inherited by subclasses.
d. Constructors can be declared as static methods.

31.What is the default constructor in Java?


a. A constructor that is automatically created by the Java compiler if no constructor is
defined.
b. A constructor with default values.
c. A constructor that takes no arguments.
d. A constructor that is always private.

32.In Java, which keyword is used to call a superclass constructor from a subclass
constructor?
a. base
b. super
c. this
d. extends

33.What is constructor overloading in Java?


a. Creating multiple classes with the same constructor name.
b. Creating a new constructor with the same name in a subclass.
c. Creating multiple constructors in a class with different parameter lists.
d. Creating a constructor that takes only one argument.

34.Which constructor is invoked when an object of a class is created using the new
keyword and without specifying any parameters?
a. Parameterized constructor
b. Default constructor
c. Private constructor
d. Static constructor

35.In Java, what is the purpose of the this keyword in a constructor?


a. It refers to the superclass.
b. It calls another constructor in the same class.
c. It refers to the current instance of the class.
d. It is used to define a new class.

36.What happens if a class does not define any constructors in Java?


a. It is not allowed in Java.
b. The class will have a default constructor provided by the Java compiler.
c. The class cannot be instantiated.
d. The class will have a private constructor.

37.In Java, which of the following statements is true regarding constructor names?
a. The constructor name must match the class name.
b. The constructor name must be "constructor."
c. The constructor name must be "new."
d. The constructor name is irrelevant.

38.What is the purpose of using the this() constructor call in Java?


a. To create a new object of the class.
b. To call another constructor in the same class with specific arguments.
c. To destroy objects.
d. To change the access modifier of a constructor.

39.What is the primary purpose of a constructor in Java?


a. To create a new class.
b. To define global variables.
c. To initialize object properties and allocate resources.
d. To execute mathematical calculations.

40.Which of the following is true about constructors in Java?


a. A class can have multiple constructors with the same parameter list.
b. Constructors can have a return type.
c. Constructors can be inherited by subclasses.
d. Constructors can be declared as static methods.
41.What is the default constructor in Java?
a. A constructor that is automatically created by the Java compiler if no constructor is
defined.
b. A constructor with default values.
c. A constructor that takes no arguments.
d. A constructor that is always private.

42.In Java, which keyword is used to call a superclass constructor from a subclass
constructor?
a. base
b. super
c. this
d. extends

43.What is constructor overloading in Java?


a. Creating multiple classes with the same constructor name.
b. Creating a new constructor with the same name in a subclass.
c. Creating multiple constructors in a class with different parameter lists.
d. Creating a constructor that takes only one argument.

44.Which constructor is invoked when an object of a class is created using the new
keyword and without specifying any parameters?
a. Parameterized constructor
b. Default constructor
c. Private constructor
d. Static constructor

45.In Java, what is the purpose of the this keyword in a constructor?


a. It refers to the superclass.
b. It calls another constructor in the same class.
c. It refers to the current instance of the class.
d. It is used to define a new class.

46.What happens if a class does not define any constructors in Java?


a. It is not allowed in Java.
b. The class will have a default constructor provided by the Java compiler.
c. The class cannot be instantiated.
d. The class will have a private constructor.

47.In Java, which of the following statements is true regarding constructor names?
a. The constructor name must match the class name.
b. The constructor name must be "constructor."
c. The constructor name must be "new."
d. The constructor name is irrelevant.

48.What is the purpose of using the this() constructor call in Java?


a. To create a new object of the class.
b. To call another constructor in the same class with specific arguments.
c. To destroy objects.
d. To change the access modifier of a constructor.

49.Certainly! Here are 10 multiple-choice questions (MCQs) related to inheritance in


Java:

Inheritance in Java:

What is inheritance in Java?


a. A way to create new objects from classes.
b. A mechanism to define multiple constructors in a class.
c. A process of acquiring the properties and behaviors of an existing class.
d. A method for creating global variables.

50,In Java, which keyword is used to declare a class as a subclass (or derived class) of
another class?
a. sub
b. extends
c. implements
d. inherits

51,What is a superclass in Java?


a. The class that inherits properties and behaviors.
b. The class that does not have any methods.
c. The base class from which another class is derived.
52,What does the "is-a" relationship represent in inheritance?
a. A relationship where an object is an instance of another object.
b. A relationship where one object is a property of another object.
c. A relationship where an object has many methods.
d. A relationship where two classes have the same constructor.

53,In Java, a subclass can access which of the following members of its superclass?
a. Public members only
b. Private members only
c. Protected members only
d. Public and protected members

54,What is the primary benefit of inheritance in Java?


a. It allows a class to have multiple constructors.
b. It enables the creation of new classes from existing classes.
c. It simplifies object creation.
d. It allows direct access to private members of the superclass.

55,In Java, what is the default access modifier for a superclass's members if no access
modifier is specified?
a. private
b. protected
c. public
d. package-private (default)

56,What is method overriding in Java?


a. Creating a new method with the same name in a subclass
b. Hiding a method from a superclass
c. Modifying the access specifier of a method
d. Calling a method from another class

57,What is the purpose of the super keyword in Java?


a. To call a method from another class.
b. To define a new class.
c. To refer to the superclass or parent class.
d. To create a new object of a class.

58,Which type of inheritance allows a class to inherit properties and behaviors from
multiple classes?
a. Single inheritance
b. Multiple inheritance
c. Multilevel inheritance
d. Hierarchical inheritance

60,What is polymorphism in Java?


a. A mechanism to define multiple constructors in a class.
b. A way to create new objects from classes.
c. The ability of a method to perform different actions based on the object that is calling
it.
d. The process of acquiring the properties and behaviors of an existing class.

Which type of polymorphism is achieved through method overloading in Java?


a. Compile-time polymorphism
b. Run-time polymorphism
c. Dynamic polymorphism
d. Static polymorphism

61,What is method overloading in Java?


a. Creating multiple methods with different names in a class.
b. Creating multiple methods with the same name but different parameters in a class.
c. Creating methods with different return types in a class.
d. Creating methods that have the same name and the same parameters in a class.
62,In Java, what is the process of selecting the appropriate method at runtime called?
a. Method overloading
b. Method overriding
c. Method declaration
d. Dynamic method selection

63,Which keyword is used in method overriding to indicate that a method in a subclass


is intended to override a method in the superclass?
a. this
b. overridden
c. super
d. @Override

64,What is the result of the following code in Java?

Parent obj = new Child();


obj.print();
(Assuming that Child is a subclass of Parent and both have a method named print.)
a. Compilation error
b. Calls the print method of the Parent class
c. Calls the print method of the Child class
d. Calls both print methods

65,In Java, what is a superclass reference variable used to reference an object of a


subclass called?
a. Superclass object
b. Subclass reference
c. Parent reference
d. Polymorphic reference

66,What is the key advantage of using polymorphism in Java?


a. It simplifies object creation.
b. It allows access to private members of a superclass.
c. It enables dynamic method selection at runtime.
d. It ensures compile-time type safety.

67,Which type of polymorphism is achieved through method overriding in Java?


a. Compile-time polymorphism
b. Run-time polymorphism
c. Static polymorphism
d. Dynamic polymorphism

Exercise 1: Write a Java program that prints all the even numbers from 1 to 20.

Exercise 2: Write a Java program to find the sum of all the numbers from 1 to 100 using
a while loop.
java

Exercise 3: Create a base class Shape with a method area(). Create two subclasses, Circle
and Rectangle, that inherit from Shape. Implement the area() method in each subclass
to calculate the area of the circle and rectangle, respectively.

Expected Output:
mathematica

Area of Circle: 78.53981633974483


Area of Rectangle: 24.0

Constructors:
Exercise 4: Create a class Person with a constructor to initialize the name and age.
Create an object of the Person class and print the name and age.
Expected Output:
makefile
Copy code
Name: John
Age: 30
Exercise 5: Write a Java program to determine whether a given number is prime or not.
Accept a number from the user and display the result.
Expected Output:
less

Enter a number: 17
17 is a prime number.

I:
Exercise 6: Create a class hierarchy for different types of animals. Define a common
method makeSound() in the base class Animal. Create subclasses for different animals
(e.g., Dog, Cat) and override the makeSound() method to make specific sounds for each
animal. Create objects of different animal types and make them make sounds.
Expected Output:
bash

The dog says Woof!


The cat says Meow!

Constructors:
Exercise 7: Create a class Book with properties like title, author, and price. Implement a
parameterized constructor to initialize these properties. Create an array of books and
display the details of the books.
Expected Output:
vbnet

Book 1:
Title: "Java Programming"
Author: John Smith
Price: $25.99

Book 2:
Title: "Data Structures and Algorithms"
Author: Alice Johnson
Price: $19.95

Exercise 8: Write a Java program to find and display all the Armstrong numbers
between 1 and 1000. An Armstrong number is a number that is equal to the sum of its
own digits, each raised to the power of the number of digits.
Expected Output:
sql
Copy code
Armstrong numbers between 1 and 1000:
1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407

Inheritance and Polymorphism:


Exercise 9: Create a base class Vehicle with properties like make, model, and year.
Create subclasses Car and Motorcycle that inherit from Vehicle. Implement a
displayInfo() method in each subclass to display information about the vehicle.
Expected Output:
yaml
Copy code
Car:
Make: Toyota
Model: Camry
Year: 2022

Motorcycle:
Make: Honda
Model: CBR500R
Year: 2021

I hope these exercises help you practice and improve your Java programming skills!
Exercise 10: Write a Java program to calculate and display the factorial of a given
number. Accept the number from the user and print the result.
Expected Output:
csharp
Copy code
Enter a number: 5
The factorial of 5 is 120.

Inheritance and Polymorphism:


Exercise 11: Create a class hierarchy for different shapes (e.g., Circle, Square, Triangle).
Define a common method calculateArea() in the base class Shape. Create subclasses for
each shape and override the calculateArea() method to calculate the area of each shape.
Calculate and display the areas of different shapes.
Expected Output:
mathematica
Copy code
Circle Area: 78.54
Square Area: 25.0
Triangle Area: 6.0

Constructors:
Exercise 12: Create a class Student with properties like name, roll number, and age.
Implement a parameterized constructor to initialize these properties. Create an array of
students and display their details.
Expected Output:
yaml
Copy code
Student 1:
Name: Alice
Roll Number: 101
Age: 18

Student 2:
Name: Bob
Roll Number: 102
Age: 19

Control Statements:
Exercise 13: Write a Java program to check and display whether a given year is a leap
year or not. Accept a year from the user and print the result.
Expected Output:
yaml
Copy code
Enter a year: 2024
2024 is a leap year.

Inheritance and Polymorphism:


Exercise 14: Create a class hierarchy for vehicles (e.g., Car, Bicycle). Define a common
method startEngine() in the base class Vehicle. Create subclasses for different types of
vehicles and override the startEngine() method to start the engine for each vehicle.
Simulate starting different vehicles' engines.
Expected Output:
makefile
Copy code
Car: Engine started.
Bicycle: Pedaling...

You might also like