Maximum Marks: 50
Time allowed: One and a half hours
Answers to this Paper must be written on the paper provided separately.
You will not be allowed to write during the first 10 minutes.
This time is to be spent reading the question paper.
The time given at the head of this Paper is the time allowed for writing the answers.
Attempt all questions from Section A and any four questions from Section B.
SECTION A
(Attempt all questions.)
It appears that this quiz is not set up correctlySection-B
(Attempt any four questions)
2. Define a class with an integer array to store 6 integers and the main function to accept 6 numbers in the array and swap the adjacent elements.
import java.util.*;
class SwapNums
{
public static void main(String args[])
{
int i, t;
int arr[] = new int[6];
Scanner sc = new Scanner(System.in);
System.out.print("Enter 6 numbers:");
for (i = 0; i < 6; i++)
{
arr[i] = sc.nextInt();
}
for(i = 0;i < 6;i=i+2)
{
t = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = t;
}
System.out.print("After swap list are:");
for (i = 0; i < 6; i++)
{
System.out.print(" " + arr[i]);
}
}
}
Output:
Enter 6 numbers:
10
20
30
40
50
60
After swap list are:20 10 40 30 60 50
3. Given the specification of the following class Student.
Class name : Student
Data members
Roll integer
Name String
Term1marks float
Term2marks float
Perc float
Member functions
getStudent() – To input student roll, name, and marks in 2 terms and calculate the percentage (Formula: Perc=(Term1+Term2)/2)
showStudent() – To display student name and percentage.
Define the class as per the specification.
import java.io.*;
class Main
{
int roll;
String name ;
float term1marks;
float term2marks;
float perc;
public void getStudent()throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
System.out.println("Enter The Roll No");
roll=Integer.parseInt(in.readLine());
System.out.println("Enter The Name");
name=in.readLine();
System.out.println("Enter The term 1 marks");
term1marks=Float.parseFloat(in.readLine());
System.out.println("Enter The term 2 marks");
term2marks= Float.parseFloat(in.readLine());
perc=(term1marks + term2marks)/2;
}
void showStudent()
{
System.out.println("Name :"+ name);
System.out.println("Percentage:"+perc);
}
public static void main(String[] args)throws IOException
{
Main ob = new Main();
ob.getStudent();
ob.showStudent();
}
}
Output:
Enter The Roll No
25
Enter The Name
Manoj Mahato
Enter The term 1 marks
85
Enter The term 2 marks
95
Name :Manoj Mahato
Percentage:90.0
4. Define a class to input a sentence and display the words that are palindromes. A palindrome is a word that is the same as its reverse.
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String st,st1,st2,st3;
char ch,ch1;
int i,j,l,l1;
System.out.println("Enter a sentence:");
st = in.nextLine();
st=st.toUpperCase();
st = st + " ";
st1 = "";
st2 = "";
st3 = "";
l = st.length();
for (i = 0; i < l; i++)
{
ch = st.charAt(i);
if (ch != ' ')
{
st1=st1+ch;
}
else
{
l1=st1.length();
for (j = 0; j < l1; j++)
{
ch1=st1.charAt(j);
st2=st2+ch1;
st3=ch1+st3;
}
if(st2.equals(st3))
System.out.println("Palindrome Word " + st2);
st1 = "";
st2 = "";
st3 = "";
}
}
}
}
Output:
Enter a sentence:
Madam Arora teaches malayalam
Palindrome Word MADAM
Palindrome Word ARORA
Palindrome Word MALAYALAM
5. Define a class to accept 10 strings and a search string. Search for the string and display whether found or not. If not found, display the proper message.
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i,f; // gt for Grand Total
String st1;
f=0;
String st[] = new String[10]; // Store Name
for (i = 0; i < 10; i++)
{
System.out.print("Enter String " + (i+1) + ": ");
st[i] = in.nextLine();
}
System.out.print("Enter String To Search : ");
st1 = in.nextLine();
for (i = 0; i < 10; i++)
{
if(st[i].equals(st1))
f=1;
}
if(f==1)
System.out.println("Srarch Found ");
else
System.out.println("Srarch Not Found ");
}
}
Output:
Enter String 1: Mom
Enter String 2: Papa
Enter String 3: Miss
Enter String 4: Teacher
Enter String 5: Programmer
Enter String 6: BeeOwn
Enter String 7: Icse
Enter String 8: Java
Enter String 9: Compiler
Enter String 10: JVM
Enter String To Search : BeeOwn
Srarch Found
6. Define a class in java to input a sentence and a word and check whether the word is present in the sentence or not.
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String st,st1,st2,st3;
char ch,ch1;
int i,j,l,f;
System.out.println("Enter a sentence:");
st = in.nextLine();
st=st.toUpperCase();
st = st + " ";
f=0;
System.out.println("Enter a word to search:");
st1 = in.nextLine();
st1=st1.toUpperCase();
st2 = "";
st3 = "";
l = st.length();
for (i = 0; i < l; i++)
{
ch = st.charAt(i);
if (ch != ' ')
{
st2=st2+ch;
}
else
{
if(st1.equals(st2))
f=1;
st1 = "";
}
}
if(f==1)
System.out.println("Srarch Found ");
else
System.out.println("Srarch Not Found ");
}
}
Output:
Enter a sentence:
Madam Arora teaches malayalam
Enter a word to search:Madam
Srarch Found
7. Define a class in Java to accept a String/Sentence and display the smallest and longest words present in the String.
Sample Input: “Rahul Dravid is a consistent player”
Sample Output:
The longest word: Consistent
The smallest word: a
import java.util.*;
public class LongestWord
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int hi,i,l,l1,lo;
String st,st1,st2,st3;
char ch;
System.out.println(“Enter a word or sentence:”);
st = in.nextLine();
st = st+' '; //Add space at end of string
st1= "";
st2 ="";
st3="";
hi=0;
lo=100;
l = st.length();
for (i = 0; i < l; i++)
{
ch = st.charAt(i);
if (ch != ' ')
{
st1=st1+ch;
}
else
{
l1=st1.length();
if(l1>hi)
{
hi=l1;
st2=st1;
}
if(l1<lo)
{
lo=l1;
st3=st1;
}
st1=" ";
}
}
System.out.println("The longest word: " + st2);
System.out.println("The lowest word: " +st3);
}
}
Output:
Enter a word or sentence:
Rahul Dravid is a consistent player
The longest word: consistent
The lowest word: a