0% found this document useful (0 votes)
2 views7 pages

Java Program Questions

The document contains multiple Java programs demonstrating various concepts, including checking for prime numbers, reversing a number, and implementing object-oriented programming principles such as encapsulation and inheritance. It also includes examples of array manipulation, such as left rotation and frequency counting of elements. Each program is structured with a main method to execute the functionality and print results to the console.

Uploaded by

thiruvengaden05
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)
2 views7 pages

Java Program Questions

The document contains multiple Java programs demonstrating various concepts, including checking for prime numbers, reversing a number, and implementing object-oriented programming principles such as encapsulation and inheritance. It also includes examples of array manipulation, such as left rotation and frequency counting of elements. Each program is structured with a main method to execute the functionality and print results to the console.

Uploaded by

thiruvengaden05
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

Prime Number

public class Main {


static boolean isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i <= [Link](n); i++)
if (n % i == 0)
return false;
return true;
}
public static void main(String[] args) {
int num = 67;
if (isPrime(num)) {
[Link](num + " is a prime number");
}
else {
[Link](num + " is not a prime number");
}
}
}

Reverse the Given Number :

public class ReverseNumberExample1


{
public static void main(String[] args)
{
int number = 987654, reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
[Link]("The reverse of the given number is: " + reverse);
}
}

// Guess The OOPS Concept

class Student {

private String name;

private int age;

public String getName() {


return name;

public void setName(String name) {

[Link] = name;

public int getAge() {

return age;

public void setAge(int age) {

[Link] = age;

public class Main {

public static void main(String[] args) {

Student student = new Student();

[Link]("Arjun");

[Link](20);

[Link]("Name: " + [Link]());

[Link]("Age: " + [Link]());

}} }

//Guess the OOPS Concept

class Animal {

void eat() {

[Link]("This animal eats food.");

}
}

class Dog extends Animal {

void bark() {

[Link]("The dog barks.");

public class Main {

public static void main(String[] args) {

Dog dog = new Dog();

[Link](); // Inherited method

[Link](); // Dog-specific method

//Guess the OOPS Concept

class Shape {

void draw() {

[Link]("Drawing a shape.");

}
}

class Circle extends Shape {

@Override

void draw() {

[Link]("Drawing a circle.");

} }

class Rectangle extends Shape {

@Override

void draw() {

[Link]("Drawing a rectangle.");

}}

public class Main {

public static void main(String[] args) {

Shape shape;

shape = new Circle();

[Link]();

shape = new Rectangle();

[Link]();

} }

//Explain the Given Program

class RotateLeft {

public static void main(String[] args) {

int [] arr = new int [] {1, 2, 3, 4, 5};

int n = 3;
[Link]("Original array: ");

for (int i = 0; i < [Link]; i++) {

[Link](arr[i] + " ");

for(int i = 0; i < n; i++){

int j, first;

first = arr[0];

for(j = 0; j < [Link]-1; j++){

arr[j] = arr[j+1];

arr[j] = first;

[Link]();

[Link]("Array after left rotation: ");

for(int i = 0; i< [Link]; i++){

[Link](arr[i] + " ");

public class Frequency {

public static void main(String[] args) {

//Initialize array

int [] arr = new int [] {1, 2, 8, 3, 2, 2, 2, 5, 1};

//Array fr will store frequencies of element


int [] fr = new int [[Link]];

int visited = -1;

for(int i = 0; i < [Link]; i++){

int count = 1;

for(int j = i+1; j < [Link]; j++){

if(arr[i] == arr[j]){

count++;

//To avoid counting same element again

fr[j] = visited;

} }

if(fr[i] != visited)

fr[i] = count; }

//Displays the frequency of each element present in array

[Link]("---------------------------------------");

[Link](" Element | Frequency");

[Link]("---------------------------------------");

for(int i = 0; i < [Link]; i++){

if(fr[i] != visited)

[Link](" " + arr[i] + " | " + fr[i]); }

[Link]("----------------------------------------"); }}

You might also like