Second Class Test Question&Solution
Second Class Test Question&Solution
1. Write a java program that accepts marks of 10 students and find the second highest
marks among all students.
import java.util.*;
class Q1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int hMarks=0, secondMarks=0;
for(int i =0;i<=10;i++)
{
System.out.println("Enter marks of students ");
int m = sc.nextInt();
if(hMarks<m)
{
secondMarks = hMarks;
hMarks = m;
}
else if(secondMarks<m)
{
secondMarks = m;
}
}
System.out.println("Second highest marks is "+secondMarks);
}
}
2. Write a java program that finds the smallest positive number that has exactly 4
factors (dividers)
import java.util.*;
public class Q2
{
public static void main(String[] args)
{
int no = 2;
while(true)
{
int count = 0;
for(int i =1;i<=no;i++)
{
if(no%i==0)
count++;
}
if(count==4)
{
System.out.println("Smallest number that has exctaly 4 factors:
"+no);
break;
}
no++;
}
}
}
11111
10001
10001
10001
11111
import java.util.*;
class Q3
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
for(int i =1;i<=n;i++)
{
for(int j = 1;j<=n;j++)
{
if(i==1||i==n||j==1||j==n)
System.out.print("1 ");
else
System.out.print("0 ");
}
System.out.println();
}
}
}
A
BC
DEF
GHIJ
import java.util.*;
public class Q4
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
char ch ='A';
for(int i =1;i<=n;i++)
{
for(int j = 1;j<=i;j++)
{
System.out.print(ch++ +" ");
}
System.out.println();
}
}
}
1
121
12321
1234321
123454321
import java.util.*;
public class Q5
{
1
212
32123
4321234
543212345
import java.util.*;
public class Q6
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
for(int i =1;i<=n;i++)
{
for(int j = 1;j<=n-i;j++)
{
System.out.print(" ");
}
for(int k = i;k>=1;k--)
{
System.out.print(k+" ");
}
for(int l = 2;l<=i;l++)
{
System.out.print(l+" ");
}
System.out.println();
}
}
}
import java.util.*;
public class Q7
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
for(int i =1;i<=n;i++)
{
for(int j = 1;j<=n;j++)
{
if(j==i || j==n-i+1)
System.out.print("* ");
else
System.out.print(j+" ");
}
System.out.println();
}
}
}
12345
54321
12345
54321
12345
import java.util.*;
public class Q8
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
for(int i =1;i<=n;i++)
{
for(int j = 1;j<=n;j++)
{
if(i%2==1)
System.out.print(j+" ");
else
System.out.print((n-j+1)+" ");
}
System.out.println();
}
}
}
import java.util.*;
public class Q9
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of character: ");
int n = sc.nextInt();
for(int i =1;i<=n;i++)
{
if(i%2==1)
{
for(int j = 1;j<=i;j++)
{
System.out.print(i+" ");
}
}
else
System.out.print(i+" "+i+" ");
}
}
}
10. Write a java program that accepts a number from the keyboard and represents a
number as Sum of Two Prime Numbers.
import java.util.*;
public class Q10
{
11. Write a java program that accepts a binary number from the keyboard handprint
the first complement of this number.
import java.util.*;
class Q11
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a positive binary number");
int n = sc.nextInt();
String s = "";
for(int i = n;i!=0;i/=10)
{
int r = i%10;
if(r==0)
s = "1"+s;
else
s = "0"+s;
}
System.out.println("Complement of "+n+" is "+s);
}
}
or
import java.util.*;
class Q11
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a positive binary number");
int n = sc.nextInt();
String s = "";
for(int i = n;i!=0;i/=10)
{
int r = i%10;
s = (++r%2)+s;
}
System.out.println("Complement of "+n+" is "+s);
}
}