Q1 Java Program to Read The Number From Standard Input
Ans1-> public class ReadNumberExample1
{
public static void main(String[] args)
{
//object of the Scanner class
Scanner sc=new Scanner([Link]);
[Link]("Enter a number: ");
//invoking nextInt() method that reads an integer input by keyboard
//storing the input number in a variable num
int num = [Link]();
//closing the Scanner after use
[Link]();
//prints the number
[Link]("The number entered by the user is: "+num);
}
}
Q2 Write a program that print your name on the screen.
Ans2->
public class PrintName {
public static void main(String[] args)
{
String name = "Your Name";
[Link]("Hello, " + name + "!");
}
}
Q3 Write a program that fine the size of int, float, double and char.
Ans-> import [Link].*;
/* how to find size of variable in java */
class SizeofdataTypes
{
public static void main(String[] args) {
[Link]("Size of int: " + ([Link] / 8) + " bytes.");
[Link]("Size of long: " + ([Link] / 8) + " bytes.");
[Link]("Size of char: " + ([Link] / 8) + " bytes.");
[Link]("Size of float: " + ([Link] / 8) + " bytes.");
[Link]("Size of double: " + ([Link] / 8) + " bytes.");
}
}
Q4 Write a program that check whether character is vowel or consonant.
Ans-> import [Link].*;
class Prep {
static void Vowel_Or_Consonant(char y)
{
if (y == 'a' || y == 'e' || y == 'i' || y == 'o'
|| y == 'u' || y == 'A' || y == 'E' || y == 'I'
|| y == 'O' || y == 'U')
[Link]("It is a Vowel.");
else
[Link]("It is a Consonant.");
}
static public void main(String[] args)
{
Vowel_Or_Consonant('W');
Vowel_Or_Consonant('I');
}
}
Q5 Write a program that find the larger number among three number.
Ans-> import [Link].*;
class PrepBytes {
// Function to find the biggest of three numbers
static int biggestOfThree(int x, int y, int z)
{
return z > (x > y ? x : y) ? z : ((x > y) ? x : y);
}
// Main driver function
public static void main(String[] args)
{
// Declaring variables for 3 numbers
int a, b, c;
// Variable holding the largest number
int largest;
a = 5;
b = 10;
c = 3;
// Calling the above function in main
largest = biggestOfThree(a, b, c);
// Printing the largest number
[Link](largest
+ " is the largest number.");
}
}
Q6 Write a program that fine the ASCII value of Character.
Ans-> public class AsciiValue
{
public static void main(String[] args)
{
char ch = 'a';
int ascii = ch;
// You can also cast char to int
int castAscii = (int) ch;
[Link]("The ASCII value of " + ch + " is: " + ascii);
[Link]("The ASCII value of " + ch + " is: " + castAscii);
}
}
Q7 Write a program that check Whether a number is palindrome or not.
Ans-> import [Link];
public class Palindrome
{
public static void main(String args[])
{
int n, m, rev = 0, x;
Scanner s = new Scanner([Link]);
[Link]("Enter the number:");
n = [Link]();
m = n;
while(n > 0)
{
x = n % 10;
rev = rev * 10 + x;
n = n / 10;
}
if(rev == m)
{
[Link](" "+m+" is a palindrome number");
}
else
{
[Link](" "+m+" is not a palindrome number");
}
}
}
Q8 Write a program that display the factor of a number.
Ans-> public class Main
{
public static void main(String[] args) {
int num = 10;
[Link]( "Factors of " + num + " are " );
// finding and printing factors b/w 1 to num
for(int i = 1; i <= num; i++)
{
if(num % i == 0)
[Link](i + " ");
}
}
}
// Time Complexity : O(n)
// Auxiliary Space : O(1)
Q9 Write a program that create star pyramid on the screen.
Ans-> import [Link].*;
// Java code to demonstrate star patterns
public class GeeksForGeeks
{
// Function to demonstrate printing pattern
public static void printStars(int n)
int i, j;
// outer loop to handle number of rows
// n in this case
for(i=0; i<n; i++)
{
// inner loop to handle number of columns
// values changing acc. to outer loop
for(j=0; j<=i; j++)
// printing stars
[Link]("* ");
// ending line after each row
[Link]();
// Driver Function
public static void main(String args[])
int n = 5;
printStars(n);
}
}
Q10 Write a program that fine the area of rectangle and triangle by using the two different
function.
Ans-> class Rectangle implements Area
{
public double Compute(double l, double b)
{
return (l*b);
}
}
class Triangle implements Area
{
public double Compute(double b, double h)
{
return (b*h/2);
}
}
public class MainArea
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
double RArea = [Link](10, 20);
[Link]("The area of the Rectangle is "+RArea);
Triangle tri = new Triangle();
double TArea = [Link](10, 20);
[Link]("The area of the triangle is "+TArea);
}
}
Q11 Write a program that make a calculator by using the function.
Ans-> import [Link];
public class SimpleCalculator {
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the first number: ");
int firstNumber = [Link]();
[Link]("Enter the second number: ");
int secondNumber = [Link]();
[Link]("Enter the type of operation you want to perform (+, -, *, /, %): ");
String operation = [Link]();
int result = performOperation(firstNumber, secondNumber, operation);
[Link]("Your answer is: " + result);
}
public static int performOperation(int firstNumber, int secondNumber, String operation)
{
int result = 0;
if ([Link]("+")) {
result = firstNumber + secondNumber;
}
else if ([Link]("-")) {
result = firstNumber - secondNumber;
}
else if ([Link]("*")) {
result = firstNumber * secondNumber;
}
else if ([Link]("%")) {
result = firstNumber % secondNumber;
}
else if ([Link]("/")) {
result = firstNumber / secondNumber;
}
else {
[Link]("Invalid operation");
}
return result;
}
}
Q12 Write a program that create the class and object.
Ans-> / Java Program for class example
class Student {
// data member (also instance variable)
int id;
// data member (also instance variable)
String name;
public static void main(String args[])
// creating an object of
// Student
Student s1 = new Student();
[Link]([Link]);
[Link]([Link]);
}
}
Q13 Write a program that take the student detail and display on the screen by the help of class.
Ans-> //program to get student details
import [Link];
public class GetStudentDetails
{
public static void main(String args[])
{
String name;
int roll, math, phy, eng;
Scanner SC=new Scanner([Link]);
[Link]("Enter Name: ");
name=[Link]();
[Link]("Enter Roll Number: ");
roll=[Link]();
[Link]("Enter marks in Maths, Physics and English: ");
math=[Link]();
phy=[Link]();
eng=[Link]();
int total=math+eng+phy;
float perc=(float)total/300*100;
[Link]("Roll Number:" + roll +"\tName: "+name);
[Link]("Marks (Maths, Physics, English): " +math+","+phy+","+eng);
[Link]("Total: "+total +"\tPercentage: "+perc);
}
Q14 Write a program that show the working of Multilevl inheritance.
Ans->class Animal {
// field and method of the parent class
String name;
public void eat() {
[Link]("I can eat");
}
}
// inherit from Animal
class Dog extends Animal {
// new method in subclass
public void display() {
[Link]("My name is " + name);
}
}
class Main {
public static void main(String[] args) {
// create an object of the subclass
Dog labrador = new Dog();
// access field of superclass
[Link] = "Rohu";
[Link]();
// call method of superclass
// using object of subclass
[Link]();
}
}
Q15 Write a program that create the package.
Ans-> package p2;
import [Link].*;
public class Sub
{
int d;
public void diff()
{
[Link]("Enter the first number: ");
Scanner scan=new Scanner([Link]);
int x=[Link]();
[Link]("Enter the second number: ");
Scanner scan1=new Scanner([Link]);
int y=[Link]();
d=x-y;
[Link]("Difference="+d);
}
}
Q16 Write a program that show the working of JDBC.
Ans-> import [Link].*;
import [Link].*;
class GFG {
public static void main(String[] args) throws Exception
String url
= "jdbc:mysql://localhost:3306/table_name"; // table details
String username = "rootgfg"; // MySQL credentials
String password = "gfg123";
String query
= "select *from students"; // query to be run
[Link](
"[Link]"); // Driver name
Connection con = [Link](
url, username, password);
[Link](
"Connection Established successfully");
Statement st = [Link]();
ResultSet rs
= [Link](query); // Execute query
[Link]();
String name
= [Link]("name"); // Retrieve name from db
[Link](name); // Print result on console
[Link](); // close statement
[Link](); // close connection
[Link]("Connection Closed....");
}
}
Q17 Write a program that function over loading. Create minimum same name 4 function.
Ans-> // Java program to demonstrate working of method
// overloading in Java
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y) { return (x + y); }
// Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
return (x + y + z);
// Overloaded sum(). This sum takes two double
// parameters
public double sum(double x, double y)
return (x + y);
// Driver code
public static void main(String args[])
Sum s = new Sum();
[Link]([Link](10, 20));
[Link]([Link](10, 20, 30));
[Link]([Link](10.5, 20.5));
}
}
Q18 Write a program that show the working of function hiding.
Ans->// Java program to demonstrate
// method Hiding in java
// Base Class
class Complex {
public static void f1()
[Link](
"f1 method of the Complex class is executed.");
}
}
// class child extend Demo class
class Sample extends Complex {
public static void f1()
[Link](
"f1 of the Sample class is executed.");
}
}
public class Main {
public static void main(String args[])
Complex d1 = new Complex();
// d2 is reference variable of class Demo that
// points to object of class Sample
Complex d2 = new Sample();
// But here method will be call using type of
// reference
d1.f1();
d2.f1();
}
}
Q19 Write a program for Interface.
Ans-> interface printable{
void print();
}
class A6 implements printable{
public void print(){[Link]("Hello");}
public static void main(String args[]){
A6 obj = new A6();
[Link]();
}
}
Q20 Write a program show the working of abstract class.
Ans->
// Java Program to implement Abstract Class
// having constructor, data member, and methods
import [Link].*;
abstract class Subject {
Subject() {
[Link]("Learning Subject");
abstract void syllabus();
void Learn(){
[Link]("Preparing Right Now!");
}
}
class IT extends Subject {
void syllabus(){
[Link]("C , Java , C++");
}
}
class GFG {
public static void main(String[] args) {
Subject x=new IT();
[Link]();
[Link]();
}
}
Q21 Write a program show the working of constructor and destructor.
Ans-> public class DestructorExample
{
public static void main(String[] args)
{
DestructorExample de = new DestructorExample ();
[Link]();
de = null;
[Link]();
[Link]("Inside the main() method");
}
protected void finalize()
{
[Link]("Object is destroyed by the Garbage Collector");
}
}