Nothing Special   »   [go: up one dir, main page]

0% found this document useful (0 votes)
38 views10 pages

JAVA Praticals Q1

The document provides sample Java code snippets for 10 questions that could be included in a Java practical exam question bank. The code snippets cover topics like finding the minimum and maximum values in an array, adding matrices, finding the index of an array element, determining if a number is prime or Armstrong, finding the second highest number in an array, calculating the area of a rectangle using classes, performing arithmetic operations on complex numbers using classes, creating a Box class with a package, implementing interfaces, and raising exceptions for out of range numbers.

Uploaded by

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

JAVA Praticals Q1

The document provides sample Java code snippets for 10 questions that could be included in a Java practical exam question bank. The code snippets cover topics like finding the minimum and maximum values in an array, adding matrices, finding the index of an array element, determining if a number is prime or Armstrong, finding the second highest number in an array, calculating the area of a rectangle using classes, performing arithmetic operations on complex numbers using classes, creating a Box class with a package, implementing interfaces, and raising exceptions for out of range numbers.

Uploaded by

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

Question1] JAVA Practical Exam Question Bank

( Any One from following will come as Question No. 1)

1) Write Java methods to find the smallest and largest number from array.

import java.util.Scanner;
class minmax
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int min=100,max=0;
int []a=new int [8];
System.out.println("Enter range of array.... ");
int n =sc.nextInt();
System.out.println("Array is.... ");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
}
System.out.println("Largest value is... "+max);
for(int i=0;i<n;i++)
{
if(min>a[i])
min=a[i];
}
System.out.println("Smallest value is... "+min);
}
}

2) Write Java methods for the following


i) accept (takes number n as input parameter) n-by-n matrix.
ii) Display()-which displays matrix.
iii) Add() –which takes two matrices as parameter and performs addition of
matrices.
import java.util.Scanner;
class matrix
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int r1,r2,c1,c2,i,j;
System.out.println("Enter number of row1...");
r1=sc.nextInt();
System.out.println("Enter number of col1...");
c1=sc.nextInt();
System.out.println("Enter number of row2...");
r2=sc.nextInt();
System.out.println("Enter number of col 2...");
c2=sc.nextInt();
int m1[][]=new int [r1] [c1];
int m2[][]=new int [r2] [c2];
int m3[][]=new int [r1] [c1];
System.out.println("Enter first Matrix....");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
m1[i][j]=sc.nextInt();
}
System.out.print("");
}
System.out.println(" First Matrix....");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
System.out.print(m1[i][j]+" ");
}
System.out.println("");
}
System.out.println("Enter Second Matrix....");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
m2[i][j]=sc.nextInt();
}
System.out.print("");
}
System.out.println("Second Matrix....");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
System.out.print(m2[i][j]+" ");
}
System.out.println("");
}
System.out.println("Addition");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
System.out.print(m3[i][j]+" ");
}
System.out.println("");
}
}
}

3) Write a Java program to find the index of an array element.

class pqr
{
public static int search(int[] a,int n)
{
for(i=0;i<a.length;i++)
{
if(a[i]==n)
return i;
}
return -1;
}
public static void main(string []args)
{
int[]a={25,14,56,15,36,56,77,18,29,49};
system.out.println("Index position of is: "+ search(a,25));
system.out.println("Index position of is: "+search(a,77));
}
}

4) Write a Java Program to find whether a number is prime/Armstrong or not.

import java.util.Scanner;
class abc
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int c=0,a,temp;
int n=sc.nextInt();
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("Armstrong....");
else
System.out.println("Not....");
}

}
import java.util.Scanner;
class bike
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n= sc.nextInt();
boolean flag = false;
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)
{
flag = true;
break;
}
}
if (!flag)
System.out.println(n+ " is a prime number.");
else
System.out.println(n + " is not a prime number.");
}
}

OR

import java.util.Scanner;
class bike
{
Scanner sc=new Scanner(System.in);
public static void main(String[] args)
{
bike b=new bike();
b.primenumber();
b.amstrong();
}
void primenumber()
{
int n=sc.nextInt();
boolean flag = false;
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)
{
flag = true;
break;
}
}
if (!flag)
System.out.println(n+ " is a prime number.");
else
System.out.println(n + " is not a prime number.");
}
void amstrong()
{
int c=0,a,temp;
int num=sc.nextInt();
temp=num;
while(num>0)
{
a=num%10;
num=num/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("Armstrong....");
else
System.out.println("Not a Amstrong....");
}
}

5) Write a Java Program to find the second-highest number in an array.

public class abc{


public static int SecondLargest(int[] a,int n)
{
int temp;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return a[n-2];
}
public static void main(string args[])
{
int a[]={1,2,5,6,3,2};
system.out.println("SecondLargest: "+secondlargest(a,6));
}
}

6) Write a program to print the area of a rectangle by creating a class named


'Area' taking the values of its length and breadth as parameters of its
constructor and having a method named 'returnArea' which returns the area
of the rectangle. Length and breadth of rectangle are entered through
keyboard.

import java.until.Scanner;
class area
{
int length;
int breadth;
area(int a,int b)
{
length=a;
breadth=b;
}
public int areareturn()
{
return length*breadth;
}
}
public class Rectangle
{
public static void main(String []args)
{
Scanner obj=new Scanner(System.in);
int a,b;
System.out.println("Enter the length of Rectangle");
a=obj.nextlnt();
System.out.println("Enter the breadth of Rectangle");
b=obj.nextln();
System.out.println("Area="+obj.areareturn(1));
}
}

7) Print the sum, difference and product of two complex numbers by creating
a class named 'Complex' with separate methods for each operation whose
real and imaginary parts are entered by user.

import java.util.Scanner;
class question
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the real part of first complex no..... ");
int r1=sc.nextInt();
System.out.println("Enter the imaginary part of first complex no.....");
//r=Real,i=Imagenary
int i1=sc.nextInt();
System.out.println("Enter the real part of Second complex no....");
int r2=sc.nextInt ();
System.out.println("Enter the imaginary part of Second complex no. : ");
int i2=sc.nextInt ();
complex c = new complex();
c.add(r1, i1, r2, i2);
c.sub(r1, i1, r2, i2);
c.mul (r1, i1, r2, i2);
}
}
class complex
{
void add(int r1, int i1, int r2, int i2)
{
int real=r1+r2;
int img=i1+i2;
System.out.println("Addition of Complex Number :"+real+"+"+img+"i");
}
void sub(int r1, int i1,int r2, int i2)
{
int real=r1-r2;
int img=i1-i2;
System.out.println("Subtraction of Complex Number :"+real+"-"+img+"i");
}
void mul (int r1, int i1, int r2, int i2)
{
int real=(r1*r2)-(i1*i2);
int img=(r2* i1)+(r1*i2);
System.out.println("Multiplication of Complex Number :" +real+"*"+img+"i");
}
}

8) Write a class Box with suitable data members and member functions.
Make use of Package to save this class.

package out;
import java.util.Scanner;
class Box
{
Scanner sc=new Scanner(System.in);
String name;
int age;
float height;
void accept()
{
name=sc.nextLine();
age=sc.nextInt();
height=sc.nextFloat();
}
void disp()
{
System.out.println("Name of a person..."+name);
System.out.println("Age of a person..."+age);
System.out.println("Height of a person..."+height);
}
public static void main(String []args)
{
Box o=new Box();
o.accept();
o.disp();
}
}

9) Write a program to implement Addition and Large interfaces in a class


Operation.
import java.util.Scanner;
interface I
{
void addition(int a,int b);
void largest(int[]array,int no);
}
class operation implements I
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
operation o=new operation();
System.out.println("First number is....");
int a=sc.nextInt();
System.out.println("Second number is....");
int b=sc.nextInt();
o.addition(a,b);
int[]array=new int[10];
System.out.println("Enter number of elments....");
int no=sc.nextInt();
System.out.println("Array is....");
for(int i=0;i<no;i++)
{
array[i]=sc.nextInt();
}
o.largest(array,no);
}
public void addition(int a,int b)
{
int c;
c=a+b;
System.out.println("Addition of number is...."+c);
}
public void largest(int[]array,int no)
{
int max=0;
int min=100;
for(int i=0;i<no;i++)
{
if(array[i]>max)
max=array[i];
}
System.out.println("Largest number is ..."+max);
}
}
10) Raise an exception if user enters number outside the range of 0 to 9.

import java.util.Scanner;
class OutRangeException extends OutRangeException
{
public OutRangeException()
{
super("Out of Range Number");
}
class question10
public static void main(string []args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter From 0 to 9");
int a=s.nextInt();
try{
if(a>0 && a<9)
{
System.out.println("You Have Entered Valid Number"+a);
}
else{
throw new OutRangeException();
}
}
catch (OutRangeException e){
System.out.println(e);
}
}
}

You might also like