0% found this document useful (0 votes)
89 views6 pages

BOARD EXAM QUESTIONS ON Arrays

The document contains Java code snippets for several programs: 1. A program to accept student names and total marks, calculate average marks, and display deviations from the average. 2. A program to input integer elements into an array, find the largest/smallest elements and sum. 3. Programs involving searching arrays for names/years and displaying results. 4. Programs performing operations on strings like encoding words to Pig Latin. 5. A program to extract the file path, name, and extension from a full file path string.

Uploaded by

AD SOLUTIONS
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)
89 views6 pages

BOARD EXAM QUESTIONS ON Arrays

The document contains Java code snippets for several programs: 1. A program to accept student names and total marks, calculate average marks, and display deviations from the average. 2. A program to input integer elements into an array, find the largest/smallest elements and sum. 3. Programs involving searching arrays for names/years and displaying results. 4. Programs performing operations on strings like encoding words to Pig Latin. 5. A program to extract the file path, name, and extension from a full file path string.

Uploaded by

AD SOLUTIONS
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/ 6

Write a program to accept name and total marks of N number of students in two single

subscript array name[] and totalmarks[ ].


Calculate and print:
(i) The average of the total marks obtained by N Number of students. [average = (sum of
total marks of all the students)/N]
(ii) (ii) Deviation of each student’s total marks with the average. [deviation = total marks
of a student – average]
import java.util.*;
public class Student
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter the number of students");
n=sc.nextInt();
String name[]=new String[n];
int total[]=new int[n],sum=0;
int i;
double avg;
System.out.println("Enter the name and total marks of each student");
for(i=0;i<n;i++)
{
name[i]=sc.nextLine();
sc.nextLine();//THIS IS NECESSARY IF THE PROGRAM NEED TO BE DONE IN
BLUEJ. FOR WRITING IN EXAM THIS LINE CAN BE OMITTED
total[i]=sc.nextInt();
sum+=total[i];
}
avg=sum/n;
System.out.println("Deviation of each student's marks from total");
for(i=0;i<n;i++)
{
System.out.println(name[i]+":"+(total[i]-avg));
}
}
}
Write a program to input integer elements into an array of size 20 and perform the following
operations:
(i) Display largest number from the array.
(ii) Display smallest number from the array.
(iii) Display sum of all the elements of the array.
import java.util.*;
public class SmallLargeArray
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[20];
int large,small,sum=0,i;
System.out.println("Enter the array elements");
for(i=0;i<20;i++)
a[i]=sc.nextInt();
small=a[0];
large=a[0];
for(i=0;i<20;i++)
{
if(a[i]>large)
large=a[i];
else if(a[i]<small)
small=a[i];
sum+=a[i];
}
System.out.println("Largest number="+large);
System.out.println("Smallest number="+small);
System.out.println("Sum="+sum);
}
}
Write a program to initialize the seven Wonders of the World along with their locations in
two different arrays. Search for a name of the country input by the user. If found, display the
name of the country along with its Wonder, otherwise display “Sorry Not Found!”.
Seven wonders — CHICHEN ITZA, CHRIST THE REDEEMER, TAJMAHAL, GREAT
WALL OF CHINA, MACHU PICCHU, PETRA, COLOSSEUM
Locations — MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Example — Country Name: INDIA Output: INDIA-TAJMAHAL Country Name: USA
Output: Sorry Not Found!
import java.util.*;
public class Stringlinearsearch
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String wonders[]={"CHICHEN ITZA", "CHRIST THE REDEEMER", "TAJMAHAL",
"GREAT WALL OF CHINA", "MACHU PICCHU", "PETRA", "COLOSSEUM"};
String locations[]={"MEXICO", "BRAZIL", "INDIA", "CHINA", "PERU",
"JORDAN", "ITALY"};
char ch;
int flag=0,i;
System.out.println("Enter the country to be searched");
String country=sc.nextLine();
country=country.toUpperCase();
for(i=0;i<7;i++)
{
if(country.compareTo(locations[i])==0)
{
System.out.println("Search Successful");
System.out.println(country+":"+wonders[i]);
flag=1;
break;
}
}
if(flag==0)
System.out.println("Sorry..not found");
}
}
Write a program to accept the year of graduation from school as an integer value from, the
user. Using the Binary Search technique on the sorted array of integers given below. Output
the message “Record exists” If the value input is located in the array. If not, output the
message “Record does not exist”. {1982, 1987, 1993, 1996. 1999, 2003, 2006, 2007, 2009,
2010}
import java.util.*;
public class RecordSearch
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int year[]={1982, 1987, 1993, 1996,1999, 2003, 2006, 2007, 2009, 2010};
int search,l=0,u=9,mid,flag=0;
System.out.println("Enter the year to be searched");
search=sc.nextInt();
while(l<=u)
{
mid=(l+u)/2;
if(search==year[mid])
{
System.out.println("Record exists");
flag=1;
break;
}
else if(search>year[mid])
l=mid+1;
else if(search<year[mid])
u=mid-1;
}
if(flag==0)
System.out.println("Record doesnot exist");
}
}

Write a program that encodes a word into Piglatin.


To translate word into a Piglatin word, convert the word into uppercase and then place the
first vowel of the original word as the start of the new word along with the remaining
alphabets.
The alphabets present before the vowel being shifted towards the end followed by “AY”.
Sample input (1) : London, Sample output (1) : ONDONLAY
Sample input (2) : Olympics, Sample output (2) : OLYMPICSAY
import java.util.*;
public class Piglatin
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
int i;
char ch;
System.out.println("Enter the string");
s=sc.nextLine();
int l=s.length();
s=s.toUpperCase();
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
break;
}
String s1=s.substring(0,i);
String s2=s.substring(i);
String s3=s2+s1+"AY";
System.out.println("The new string is "+s3);
}
}

Write a program to accept the names of 10 cities in a single dimension string array and their
STD (Subscribers Trunk Dialing) codes in another single dimension integer array. Search for
a name of a city input by the user in the list. If found, display* “Search successful” and print
the name of the city along with its STD code, or else display the message “Search
Unsuccessful, No such city in the list”.
import java.util.*;
public class CitySearch
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String city[]=new String[10];
int std[]=new int[10];
String cityname;
int flag=0,i;
System.out.println("Enter the city names in capital and the std codes");
for(i=0;i<10;i++)
{
city[i]=sc.nextLine();
std[i]=sc.nextInt();
}
System.out.println("Enter the city to be searched");
cityname=sc.nextLine();
cityname=cityname.toUpperCase();
for(i=0;i<10;i++)
{
if(cityname.compareTo(city[i])==0)
{
System.out.println("Search Successful");
System.out.println(cityname+":"+std[i]);
flag=1;
break;
}
}
if(flag==0)
System.out.println("Search unsuccessful. No such city in the list");
}
}
Write a program to assign a full path and file name as given below. Using library functions,
extract and output the file path, file name and file extension separately as shown.
Input
C: / Users / admin / Pictures / flower.jpg
Output
path: C: / Users/admin/Pictures/
File name: flower
Extension: jpg

import java.util.*;
public class PathExtract
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter the string");
s=sc.nextLine();
int slash=s.lastIndexOf('/');
String path=s.substring(0,slash+1);
int dot=s.indexOf('.');
String filename=s.substring(slash+1,dot);
String extension=s.substring(dot+1);
System.out.println("Path - "+path);
System.out.println("File name - "+filename);
System.out.println("Extension - "+extension);
}
}
Output
Enter the string
C: / Users / admin / Pictures / flower.jpg
Path - C: / Users / admin / Pictures /
File name - flower
Extension – jpg

You might also like