Problems on Various Topics
Class and Objects
1.Create a class Student
with properties name
, age
, and grade
. Write a program to create an object of Student
and display its properties.
- Expected Output:
Name: John Age: 20 Grade: A
2.Create a class Rectangle
with properties length
and width
. Write a method calculateArea
to calculate the area of the rectangle. Create an object of Rectangle
, set its properties, and display the area.
- Expected Output:
Length: 5 Width: 3 Area: 15
3. Create a class Circle
with a property radius
and a method calculateCircumference
to calculate the circumference. Create an object of Circle
, set its properties, and display the circumference.
- Expected Output:
Radius: 7 Circumference: 43.96
4. Create a class Book
with properties title
, author
, and price
. Write a program to create an object of Book
and display its properties.
- Expected Output:
Title: Java Programming Author: James Gosling Price: 499
5. Create a class Car
with properties model
, year
, and price
. Write a program to create an object of Car
and display its properties.
- Expected Output:
Model: Tesla Model 3 Year: 2020 Price: 35000
Variables and Data Types
6. Write a program to declare variables of different data types (int, float, double, char, boolean) and initialize them. Print the values of these variables.
- Expected Output:
Integer: 10 Float: 5.75 Double: 19.99 Character: A Boolean: true
7. Write a program to swap two variables without using a third variable.
- Input:
a = 5, b = 3
- Expected Output:
After swapping: a = 3, b = 5
8. Write a program to demonstrate the use of type casting in Java. Convert a double to an int and print both values.
- Input:
doubleValue = 10.99
- Expected Output:
Double value: 10.99 Integer value: 10
9. Write a program to demonstrate the use of constants in Java. Declare a constant PI with the value 3.14 and use it to calculate the area of a circle.
- Input:
radius = 5
- Expected Output:
Area of the circle: 78.5
Pattern Matching
- Write a program to print the following pattern:
- Pattern:
* * * * * * * * * * * * * * *
- Expected Output:
* * * * * * * * * * * * * * *
- Write a program to print the following pattern:
- Pattern:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
- Expected Output:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
- Write a program to print the following pattern:
- Pattern:
A A B A B C A B C D A B C D E
- Expected Output:
A A B A B C A B C D A B C D E
- Write a program to print the following pattern:
- Pattern:
1 2 3 4 5 6 7 8 9 10
- Expected Output:
1 2 3 4 5 6 7 8 9 10
- Write a program to print the following pattern:
- Pattern:
5 4 3 2 1 4 3 2 1 3 2 1 2 1 1
- Expected Output:
5 4 3 2 1 4 3 2 1 3 2 1 2 1 1
- Write a program to print the following pattern:
- Pattern:
A B C D E F G H I J
- Expected Output:
A B C D E F G H I J
- Pattern:
Operators
- Write a program to perform arithmetic operations (addition, subtraction, multiplication, division) on two numbers.
- Input:
a = 10, b = 5
- Expected Output:
Addition: 15 Subtraction: 5 Multiplication: 50 Division: 2
- Input:
- Write a program to demonstrate the use of relational operators by comparing two numbers.
- Input:
a = 10, b = 5
- Expected Output:
a > b: true a < b: false a == b: false a != b: true a >= b: true a <= b: false
- Input:
- Write a program to demonstrate the use of logical operators. Check if a number is between 10 and 20 and if it is even.
- Input:
number = 12
- Expected Output:
Number is between 10 and 20: true Number is even: true
- Input:
- Write a program to demonstrate the use of bitwise operators. Perform AND, OR, and XOR operations on two numbers.
- Input:
a = 6, b = 4
- Expected Output:
a & b: 4 a | b: 6 a ^ b: 2
- Input:
- Write a program to demonstrate the use of increment and decrement operators.
- Input:
a = 5
- Expected Output:
a++: 5 ++a: 7 a--: 7 --a: 5
- Input:
Conditional Statements
- Write a program to check if a number is even or odd.
- Input:
number = 7
- Expected Output:
7 is odd
- Input:
- Write a program to find the largest of three numbers using if-else statements.
- Input:
a = 10, b = 20, c = 15
- Expected Output:
Largest number is 20
- Input:
- Write a program to check if a year is a leap year.
- Input:
year = 2020
- Expected Output:
2020 is a leap year
- Input:
- Write a program to determine the grade of a student based on marks obtained.
- Input:
marks = 85
- Expected Output:
Grade: A
- Input:
- Write a program to find the smallest of four numbers using nested if-else statements.
- Input:
a = 10, b = 20, c = 5, d = 15
- Expected Output:
Smallest number is 5
- Input:
- Write a program to check if a character is a vowel or consonant.
- Input:
char = 'a'
- Expected Output:
a is a vowel
- Input:
- Write a program to check if a number is positive, negative, or zero.
- Input:
number = -5
- Expected Output:
-5 is negative
- Input:
Loops
- Write a program to print the first 10 natural numbers using a for loop.
- Expected Output:
1 2 3 4 5 6 7 8 9 10
- Write a program to calculate the factorial of a number using a while loop.
- Input:
number = 5
- Expected Output:
Factorial of 5 is 120
- Input:
- Write a program to print all even numbers between 1 and 50 using a for loop.
- Expected Output:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
- Write a program to find the sum of all odd numbers between 1 and 100 using a while loop.
- Expected Output:
Sum of odd numbers: 2500
- Write a program to reverse a number using a while loop.
- Input:
number = 12345
- Expected Output:
Reversed number: 54321
- Input:
- Write a program to print the Fibonacci series up to N terms using a for loop.
- Input:
N = 10
- Expected Output:
0 1 1 2 3 5 8 13 21 34
- Input:
- Write a program to print the multiplication table of a number using a for loop.
- Input:
number = 5
- Expected Output:
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
- Input:
- Write a program to check if a number is prime using a for loop.
- Input:
number = 29
- Expected Output:
29 is a prime number
- Input:
- Write a program to find the greatest common divisor (GCD) of two numbers using a while loop.
- Input:
a = 48, b = 18
- Expected Output:
GCD: 6
- Input:
- Write a program to print the sum of digits of a number using a while loop.
- Input:
number = 1234
- Expected Output:
Sum of digits: 10
- Input:
Arrays
- Write a program to find the largest element in an array.
- Input:
arr = {10, 20, 30, 40, 50}
- Expected Output:
Largest element is 50
- Input:
- Write a program to reverse an array.
- Input:
arr = {1, 2, 3, 4, 5}
- Expected Output:
Reversed array: {5, 4, 3, 2, 1}
- Input:
- Write a program to find the sum of all elements in an array.
- Input:
arr = {1, 2, 3, 4, 5}
- Expected Output:
Sum of elements: 15
- Input:
- Write a program to find the second largest element in an array.
- Input:
arr = {10, 20, 30, 40, 50}
- Expected Output:
Second largest element is 40
- Input:
- Write a program to sort an array in ascending order.
- Input:
arr = {5, 3, 8, 1, 2}
- Expected Output:
Sorted array: {1, 2, 3, 5, 8}
- Input:
- Write a program to merge two arrays into a third array.
- Input:
arr1 = {1, 2, 3}, arr2 = {4, 5, 6}
- Expected Output:
Merged array: {1, 2, 3, 4, 5, 6}
- Input:
- Write a program to find the common elements between two arrays.
- Input:
arr1 = {1, 2, 3, 4}, arr2 = {3, 4, 5, 6}
- Expected Output:
Common elements: {3, 4}
- Input:
- Write a program to remove duplicate elements from an array.
- Input:
arr = {1, 2, 2, 3, 4, 4, 5}
- Expected Output:
Array without duplicates: {1, 2, 3, 4, 5}
- Input:
- Write a program to find the frequency of each element in an array.
- Input:
arr = {1, 2, 2, 3, 3, 3, 4}
- Expected Output:
Element 1: 1 time(s) Element 2: 2 time(s) Element 3: 3 time(s) Element 4: 1 time(s)
- Input:
Strings
- Write a program to accept a string from the user and print it.
- Input:
My name is John
- Expected Output:
My name is John
- Input:
- Write a program to accept a string from the user and count the number of vowels in it.
- Input:
Hello World
- Expected Output:
Number of vowels: 3
- Input:
- Write a program to accept a string from the user and count the number of words in it.
- Input:
Java is fun
- Expected Output:
Number of words: 3
- Input:
- Write a program to accept a string from the user and reverse it.
- Input:
Hello
- Expected Output:
Reversed string: olleH
- Input:
- Write a program to accept a string from the user and check if it is a palindrome.
- Input:
level
- Expected Output:
The string is a palindrome
- Input:
- Write a program to convert a string to uppercase without using library functions.
- Input:
hello
- Expected Output:
HELLO
- Input:
- Write a program to convert a string to lowercase without using library functions.
- Input:
HELLO
- Expected Output:
hello
- Input:
- Write a program to find the length of a string without using library functions.
- Input:
Hello
- Expected Output:
Length of string: 5
- Input:
- Write a program to count the number of uppercase, lowercase, and digits in a string.
- Input:
Abc123
- Expected Output:
Uppercase: 1 Lowercase: 2 Digits: 3
- Input:
- Write a program to replace all spaces in a string with underscores.
- Input:
Hello World
- Expected Output:
Hello_World
- Input:
- Write a program to find the first occurrence of a character in a string.
- Input:
Hello World, l
- Expected Output:
Character 'l' found at position 2
- Input:
- Write a program to find the last occurrence of a character in a string.
- Input:
Hello World, l
- Expected Output:
Character 'l' found at position 9
- Input:
- Write a program to check if two strings are anagrams.
- Input:
listen, silent
- Expected Output:
The strings are anagrams
- Input:
- Write a program to declare two integer variables and print their sum.
- Input:
a = 5, b = 10
- Expected Output:
Sum: 15
- Input:
- Write a program to calculate the area of a circle with a given radius.
- Input:
radius = 7
- Expected Output:
Area: 153.93804002589985
- Input:
- Write a program to swap two numbers without using a third variable.
- Input:
a = 5, b = 10
- Expected Output:
a: 10, b: 5
- Input:
- Write a program to find the average of three numbers.
- Input:
a = 5, b = 10, c = 15
- Expected Output:
Average: 10.0
- Input:
- Write a program to demonstrate the use of arithmetic operators.
- Input:
a = 10, b = 5
- Expected Output:
Addition: 15 Subtraction: 5 Multiplication: 50 Division: 2 Modulus: 0
- Input:
- Write a program to demonstrate the use of relational operators.
- Input:
a = 10, b = 5
- Expected Output:
a > b: true a < b: false a >= b: true a <= b: false a == b: false a != b: true
- Input:
- Write a program to demonstrate the use of logical operators.
- Input:
a = true, b = false
- Expected Output:
a && b: false a || b: true !a: false
- Input:
- Write a program to demonstrate the use of bitwise operators.
- Input:
a = 5, b = 3
- Expected Output:
a & b: 1 a | b: 7 a ^ b: 6 ~a: -6 a << 1: 10 a >> 1: 2
- Input:
- Write a program to check if a number is even or odd.
- Input:
number = 7
- Expected Output:
7 is odd
- Input:
- Write a program to find the largest of three numbers.
- Input:
a = 10, b = 15, c = 5
- Expected Output:
Largest number is 15
- Input:
- Write a program to check if a year is a leap year.
- Input:
year = 2020
- Expected Output:
2020 is a leap year
- Input:
- Write a program to check if a character is a vowel or consonant.
- Input:
char = 'e'
- Expected Output:
e is a vowel
- Input:
- Write a program to print the first 10 natural numbers using a for loop.
- Expected Output:
1 2 3 4 5 6 7 8 9 10
- Write a program to calculate the factorial of a number using a while loop.
- Input:
number = 5
- Expected Output:
Factorial of 5 is 120
- Input:
- Write a program to print all even numbers between 1 and 50 using a for loop.
- Expected Output:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
- Write a program to find the sum of all odd numbers between 1 and 100 using a while loop.
- Expected Output:
Sum of odd numbers: 2500
- Write a program to reverse a number using a while loop.
- Input:
number = 12345
- Expected Output:
Reversed number: 54321
- Input:
- Write a program to print the Fibonacci series up to N terms using a for loop.
- Input:
N = 10
- Expected Output:
0 1 1 2 3 5 8 13 21 34
- Input:
- Write a program to print the multiplication table of a number using a for loop.
- Input:
number = 5
- Expected Output:
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
- Input:
- Write a program to check if a number is prime using a for loop.
- Input:
number = 29
- Expected Output:
29 is a prime number
- Input:
- Write a program to find the greatest common divisor (GCD) of two numbers using a while loop.
- Input:
a = 48, b = 18
- Expected Output:
GCD: 6
- Input:
- Write a program to print the sum of digits of a number using a while loop.
- Input:
number = 1234
- Expected Output:
Sum of digits: 10
- Input:
- Write a program to find the largest element in an array.
- Input:
arr = {10, 20, 30, 40, 50}
- Expected Output:
Largest element is 50
- Input:
- Write a program to reverse an array.
- Input:
arr = {1, 2, 3, 4, 5}
- Expected Output:
Reversed array: {5, 4, 3, 2, 1}
- Input:
- Write a program to find the sum of all elements in an array.
- Input:
arr = {1, 2, 3, 4, 5}
- Expected Output:
Sum of elements: 15
- Input:
- Write a program to find the second largest element in an array.
- Input:
arr = {10, 20, 30, 40, 50}
- Expected Output:
Second largest element is 40
- Input:
- Write a program to sort an array in ascending order.
- Input:
arr = {5, 3, 8, 1, 2}
- Expected Output:
Sorted array: {1, 2, 3, 5, 8}
- Input:
- Write a program to merge two arrays into a third array.
- Input:
arr1 = {1, 2, 3}, arr2 = {4, 5, 6}
- Expected Output:
Merged array: {1, 2, 3, 4, 5, 6}
- Input:
- Write a program to find the common elements between two arrays.
- Input:
arr1 = {1, 2, 3, 4}, arr2 = {3, 4, 5, 6}
- Expected Output:
Common elements: {3, 4}
- Input:
- Write a program to remove duplicate elements from an array.
- Input:
arr = {1, 2, 2, 3, 4, 4, 5}
- Expected Output:
Array without duplicates: {1, 2, 3, 4, 5}
- Input:
- Write a program to find the frequency of each element in an array.
- Input: `arr = {1, 2, 2, 3, 3,
- Jacoco - January 3, 2025
- Dependency Track – End To End CI/CD Pipeline - November 29, 2024
- Dependency-track Jenkins Integration - November 27, 2024