Core JAVA
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
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
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
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
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
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]
32.In Java, which keyword is used to call a superclass constructor from a subclass
constructor?
a. base
b. super
c. this
d. extends
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
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.
42.In Java, which keyword is used to call a superclass constructor from a subclass
constructor?
a. base
b. super
c. this
d. extends
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
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.
Inheritance in Java:
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
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
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)
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
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
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
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
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.
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.