STD X - Ii Model - Jan 2017

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

NEW HORIZON PUBLIC SCHOOL

INDIRANAGAR, BANGALORE 560 008.


SECOND MODEL EXAMINATION JANUARY 2017
COMPUTER APPLICATIONS

STD. : X MAX MARKS :


100
DATE : 17/01/2017 TIME : 2 HOURS

Answer to this paper must be written on the paper provided separately.


You will not be allowed to write during the first 15 minutes.
This time is to be spent in reading the question paper.
The time given at the head of this paper is the time allowed for writing the
answers.
This paper is divided into two sections.
Attempt all questions from Section A and any four questions from Section B.
The intended marks for questions or parts of questions are given in brackets [ ].

SECTION A (40 Marks)


Attempt all questions

Question 1.
(a) Why are objects called Instance of a class? What is the data type of
an object?
[2]
(b)Define Encapsulation. How is this concept implemented in a Java
program? [2]
(c) Differentiate between the two types of data stream in Java.
[2]
(d)Differentiate between Pageables and Printables modules of printing.
[2]
(e) What is temporary instance? Give an example.
[2]
Question 2.
(a) (i) Explain the significance of the final keyword when used to
declare a-
(I) class (II) variable
(ii) (I) Name a reserved word that is not a keyword.
(II) Name a keyword that links a sub class to its super class.
[2]
(b)(i) Name the access specifiers that can be used with a
(I) class declaraion (II) local variable declaration
(ii) Which access specifier has a wider range of visibility between
package and protected? [2]

1
(c) Differentiate between hasNext(), hasNextInt() and nextInt() methods
of the Scanner class.
[2]
(d)(i) Name the Math class method that is most suitable to get the
answer
as stated for the following calculations-
(I) To display the number of people required to do a job
output of a complex calculation using double values. The
method used may return a double value, but it should be a
whole number that is either same or higher than the
argument.
(II) To display the answer of a subtraction as a positive value.
(ii) State the utility of the instanceof operator and its return data
type. [2]

(e) Name the following exception handling keywords


(i) Keywords used to handle an exception and continue with the
program execution.
(ii) A keyword that includes a block that gets executed whether the
program encounters an exception or not.
(iii) A keyword that is used to write a user-defined exception.
[2]
Question 3.
(a) What will be the content of the array assuming the inputs are 6, 5, 9,
2:
boolean ans [] = new boolean [ 4 ] ; int numb , pNumb = 0 ;
System . out . println ( Enter 4 integers : ) ;
for ( int i = 0 ; i < 4 ; i ++ )
{ numb = scan . nextInt ( ) ;
if ( i != 0 ) ans [ i ] = pNumb > numb ;
pNumb = numb ; } [2]
(b)Consider the following prototypes and answer the given questions-
(A) public void check ( long numbs [ ] )
(B) public void check ( long n [ ] )
(C) public long check ( long numb )
(D) public void check ( long n )
(i) Identify two prototypes which may be considered to be
overloaded.
(ii) Identify an accessor method and a mutator method.
[2]
(c) Explain the function of each of the four underlined parts in the
following statement-
Stock item1 = new Stock ( ) ;
[2]

2
(d)What will be the values stored in a and b after the following
statement are executed?
double a = 3 , b = -7 ;
a*=(int) (Math.pow(a- -, a)%Math.abs(b ++ +a ++)/Math.sqrt(+
+a)); [2]
(e) Write the following method call statements
(i) To call a non-static method add ( ) stored in a class named
Numbers.
(ii) To call a static method display ( ) stored in a class named Series.
[2]
(f) What will be the output of the following?
int x = 0 , y = 0 ;
for ( x = 1 ; x <= 6 ; x +=3 )
for ( y = 15 ; y >1 ; y -= x*7 )
System . out . println ( y - x ) ; System . out . println ( y ) ;
[2]
(g)What will be the output of :
String a = Dont limit your challenges. Challenge your limits. ;
System.out.println(String.valueOf (a.compareTo (a)+a.charAt (0))+
(a.substring(a.indexOf ('c') , a.lastIndexOf('e')).endsWith("ge")));
[2]
(h)Write an equivalent Java expression for the following :
b b24 ac
[2] 2a

(i) Write a program fragment using if else statement which cannot be


replaced by a switch case statement or by one statement using a
ternary operator. [2]

(j) State why each of the following assignments result in compile-time


error-
(i) long l = 6046531767 ; (ii) float f = 12.34 ;
(iii) char c = c ; c = c + 2 ; (iv) String str = 2 + 3 ;
[2]

SECTION B (60 Marks)


Attempt any four questions from this Section.
The answers in this Section should consist of the Programs in either BlueJ
environment or any program environment with Java as the base.
Each program should be written using Variable descriptions/Mnemonic
Codes such that the logic of the program is clearly depicted.
Flow-Charts and Algorithms are not required.

Question 4.
Write a class having the name BackPack to purchase backpacks for kids
on-line. The class should have the following members:

3
Data members:
Instance variables :
String uName : To store the name of the buyer
long uMobNo : To store the mobile number of the buyer
int bagType : To store the type of bag chosen
int qty : To store the number bags to be purchased
String rName : To store the name of the recipient
String address : To store the address of the recipient
long rMobNo : To store the mobile number of the recipient
Class variables :
int rate1 , rate2 , rate3 : To store the rate of the three types of
bag
Member methods:
Backpack ( ) : Parameterised constructor to initialise the instance
variables with the accepted values
calcAmt ( ) : Calculates the payable amount.
The type and the price list of the bags is as follows:
Type Description Rate
1 Teddy soft toy bag ` 260
2 Bunny bag ` 399
3 Felt Velvet bag ` 499
display ( ) : Displays all the details and the total amount to be paid.
main ( ) : Create an object of the class and calls the above
methods. [15]
Question 5.
Write a menu-driven program to display one of the following formats as
per the users choice after accepting the required details:

4
(i) E.g. 1 Choice : 1 (ii) E.g. 2 Choice : 2
No. of lines : 6 Input word : Avatar
0 A
01 Av
024 Ava
0369 Avat
0481216 Avata
0510152025 Avatar
[15]

5
Question 6.
Write a program in Java to store the name, country and the time taken to
complete the Rio de Janeiro Olympics marathon event by the following
two athletes in six variables. Calculate the average of the two
times clocked. Display all the details as shown below:
Description Name Country Time taken
Participant 1 Eliud Kipchoge Kenya 2:08:44
Participant 2 T. Gopi India 2:15:25
-------------------------------------------------------------------------------------------
Average Time _:__:__
------------------------------------------------------------------------------------------- [15]

Question 7.
Write a program in Java to accept 10 integers in an array. Delete the
duplicate elements. Display the array content before and after deletion.

Input : 2 , 33 , 9 , 78 , 10 , 45 , 9 , 48 , 10 , 2
Output : 2 , 33 , 9 , 78 , 10 , 45 , 48
[15]

Question 8.
Using overloaded methods calculateSum ( ), write a program in Java to
calculate the sum of the following series:
(i) s= 1 - 5 + 9 - 13 + 17 - 21
9 17 25 33 41
(ii) s = 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! +n terms
[15]

Question 9.
Using user-defined methods write a program in Java to accept a string and
sort the words in ascending order of alphabets.
Input : If you litter, The future will be bitter
Output : If The be bitter future litter, will you
[15]

KEY TO THE QUESTIONS


NEW HORIZON PUBLIC SCHOOL
INDIRANAGAR, BANGALORE 560 008.
FIRST TERMINAL EXAMINATION SEPTEMBER 2016
COMPUTER APPLICATIONS
STD. : X MAX MARKS :
100
DATE : 23/09/2016 TIME : 2 HOURS

6
Answer to this paper must be written on the paper provided separately.
You will not be allowed to write during the first 15 minutes.
This time is to be spent in reading the question paper.
The time given at the head of this paper is the time allowed for writing the
answers.
This paper is divided into two sections.
Attempt all questions from Section A and any four questions from Section B.
The intended marks for questions or parts of questions are given in brackets [ ].

SECTION A (40 Marks)


Attempt all questions

Question 1.
(a) Why are objects called Instance of a class? What is the data type of
an object?
[2]
**** Instance refers to an item that is typical of a class or group. As an
object represents all the characteristics and behaviour defined in the
class, it is regarded as an instance of the class.
A class (in which usually its instance variables are declared) /
reference / user-defined / composite.
[1+1]
(b)Define Encapsulation. How is this concept implemented in a Java
program? [2]
****Encapsulation in Java is a mechanism of wrapping the data (variables)
and code acting on the data (methods) together as a single unit.
Wrapping up data and methods in a single class, using access
specifiers. [1+1]
(c) Differentiate between the two types of data stream in Java.
[2]
Byte stream Character stream
Occupies 1 byte/character. Occupies 2 bytes/character.
Reads/writes binary data. Reads/writes Unicode characters.
[1+1]
(d) Differentiate between Pageables and Printables modules of printing.
[2]
Pageables Printables

Each page can feature a One page painter for


entire
Different layout. document.
Can print pages out of sequence. Pages rendered in sequence.
[1+1]

(e) What is temporary instance? Give an example.


[2]

7
****A temporary instance refers to an anonymous object created
temporarily to use once. This optimises the memory space.
E.g. BufferedReader br = new BufferedReader
( new InputStreamReader ( System . in ) ) ;
Temporary instance
[1+1]
Question 2.
(a) (i) Explain the significance of the final keyword when used to
declare a-
(III) class (II) variable
[1]
****(i) (I) Class The class cannot be inherited.
(II) Variable The variable contains a constant value. [+]
(ii) (I) Name a reserved word that is not a keyword.
(II) Name a keyword that links a sub class to its super class.
[1]
****(ii) (I) true/false/null (II) extends
[+]
(b)(i) Name the access specifiers that can be used with a
(II) class declaraion (II) local variable declaration
[1]
**** (i) (I) public, package/friendly/default (II) None.
[+]
(ii) Which access specifier has a wider range of visibility between
package and protected? [1]
**** (ii) protected
[+]
(c) Differentiate between hasNext(), hasNextInt() and nextInt() methods
of the Scanner class.
[2]
**** Point hasNext()
hasNextInt() nextInt()
Return value boolean boolean int
Advacement past input No No
Yes
Function Checks whether Checks
whether Accepts an
any more tokens any more int int value
are there in the tokens are there from the user
buffer in the buffer
[2]
(d)(i) Name the Math class method that is most suitable to get the
answer

8
as stated for the following calculations-
(I) To display the number of people required to do a job
output of a complex calculation using double values. The
method used may return a double value, but it should be a
whole number that is either same or higher than the
argument.
(II) To display the answer of a subtraction as a positive value.
[1]
**** (i) (I) Math . ceil ( ) (II) Math . abs ( )
[+]
(ii) State the utility of the instanceof operator and its return data
type. [1]
**** The instanceof operator is used to test whether the specified object
is an instance of the specified class or not. Return type boolean.
[+]
(e) Name the following exception handling keywords
(i) Keywords used to handle an exception and continue with the
program execution.
(ii) A keyword that includes a block that gets executed whether the
program encounters an exception or not.
(iii) A keyword that is used to write a user-defined exception.
[2]
**** (i) try and catch (ii) finally (iii) throw
[+++]

Question 3.
(a) What will be the content of the array assuming the inputs are 6, 5, 9,
2:
boolean ans [] = new boolean [ 4 ] ; int numb , pNumb = 0 ;
System . out . println ( Enter 5 integers : ) ;
for ( int i = 0 ; i < 5 ; i ++ ) . 1
{ numb = scan . nextInt ( ) ; 2
if ( i != 0 ) ans [ i ] = pNumb > numb ; 3
pNumb = numb ; } 4
[2]
**** ans [ ] = { false , false , false , false }
Line no. pNumb i i<5 numb ans [ i ] i!=0
pNumb>numb
1 0 0 true - [0]false - -
2 0 0 - 6 [0]false - -
3 0 0 - 6 [0]false false -
4 6 0 - 6 [0]false - -

9
1 6 1 true 6 [1]false - -
2 6 1 - 5 [1]false - -
3 6 1 - 5 [1]true true true
4 5 1 - 5 [1]true - -
1 5 2 true 5 [2]false - -
2 5 2 - 9 [2]false - -
3 5 2 - 9 [2]false true false
4 9 2 - 9 [2]false - -
1 9 3 true 9 [3]false - -
2 9 3 - 2 [3]false - -
3 9 3 - 2 [3]true true true
4 2 3 - 2 [3]true - -
1 2 4 true 2 - - -
2 2 3 - Waits for input. If a value is entered-
3 2 3 - ? AIOOB - -
Ans: false true false true
ArrayIndexOutOfBoundsException
[+++]
(b)Consider the following prototypes and answer the given questions-
(A) public void check ( long numbs [ ] )
(B) public void check ( long n [ ] )
(C) public long check ( long n [ ] )
(D) public void check ( long n )
(i) Identify two prototypes which may be considered to be
overloaded. [1]
**** (A) or (B) or (C) and (D) because the signature (parameter list an
array reference of long data type) of the first three are the same, they
cannot be considered as overloaded methods.
[+]
(ii) Identify an accessor method and a mutator method.
[1]
**** Consider the following code
import java . util . Scanner ;
class Details
{ private String name ; private long admnNo ;
public void setDetails ( )
{ Scanner scan = new Scanner (System.in) ;
System.out.println ( Enter name and admission number
: );
name = scan . nextLine ( ) ; admnNo = scan.nextLong();

10
} //Mutator method sets the values in the private instance
variables
public long getAdmnNo ( String n )
{ if ( n . equalsIgnoreCase ( name ) ) { return admnNo ; }
else return 0 ;
} //Accessor method used to send back a value of a private
variable
}
import java . util . Scanner ;
class Students
{ public static void main ( )
{ Scanner scan = new Scanner (System.in) ;
Details student1 = new Details ( ) ;
student1 . setDetails ( ) ;
.
System.out.println
(Enter the name to search for the adm. No.: );
String name = scan . nextLine ( ) ;
long an = student1 . getAdmnNo ( name ) ;
if ( an != 0 )
System.out.println ( "Admission number : " + an );
else System.out.println ( "Name not found." ) ;

}
}
So, mutator methods are used to change a value. They may or may
not return value, whereas accessor methods are used to get a value
of a private variable to a method in a different class. So, they always
return value.
Ans : Accessor (C) Mutator (A) or (B) or (C) or (D) [+]
(c) Explain the function of each of the four underlined parts in the
following statement-
Stock item1 = new Stock ( ) ;
[2]
**** Stock item1 = Declares an object of Stock class by the name - item1.
= - Assigns the reference of the object in the reference variable
item1.
new Creates a space in the primary memory for the object.
Stock ( ) Calls the constructor to initialise the instance variables of
the object
with default values.
[+++]
(d)What will be the values stored in a and b after the following
statement are executed?

11
double a = 3 , b = -7 ;
[2]
a*=(int) (Math.pow(a- -, a) %Math.abs(b ++ +a ++)
/Math.sqrt(++a));
**** 3*=(int)(Math.pow(3.0(a=2.0) , 2.0)%Math.abs (-7.0 (b=-6.0)+
2.0(a=3.0))/Math.sqrt(4.0))
= (int) ( 9.0 %5.0 / 2.0 )
= (int) ( 4.0 / 2.0 ) = (int) (2.0) = 2 -> 3.0*2 = 6.0
Ans: a = 6.0, b = -6.0
[1+1]
(e) Write the following method call statements
(i) To call a non-static method add ( ) stored in a class named
Numbers. [1]
**** obj . add ( ) // from a method in the same class
or
Numbers . obj . add ( ) // from a method in a different class
[1]
(ii)To call a static method display ( ) stored in a class named Series.
[1]
**** Series . display ( ) // from a method in a different class
or
display ( ) // from a method in the same class [1]
(f) What will be the output of the following?
int x = 0 , y = 0 ;
for ( x = 1 ; x <= 6 ; x +=3 )
for ( y = 15 ; y >1 ; y -= x*7 )
System . out . println ( y - x ) ; System . out . println ( y ) ;
[2]
Line no. x x<=6 y y>1
yx display
1 1 true 0 - - -
-
2 1 - 15 true - -
3 1 - 15 - 15-1=14 14
2 1 - 1*7=7,15-7=8 true --
3 1 - 8 - 8-1=7 7
2 1 - 1*7=7,8-7=1 false --
1 4 true 1 -- -
2 4 - 15true --
3 4 - 15- 15-4=11 11
2 4 - 4*7=28,15-28=-13 false --
1 7 false -13 -- -
4 7 - -13 -- -13
Ans: 14
7
11
-13 [+++]
(g)What will be the output of :
String a = Dont limit your challenges. Challenge your limits. ;

12
System.out.println(String.valueOf (a.compareTo (a)+a.charAt (0))+
(a.substring(a.indexOf ('c') , a.lastIndexOf('e')).endsWith("ge")));
[2]
**** System.out.println(String.valueOf (a.compareTo (a)+a.charAt (0))+
String.valueOf( (0 + 68 ) +
(a.substring (a . indexOf ('c') , a . lastIndexOf('e') ) .
endsWith("ge")));
(a.substring (17(c of challenges) ,37(last e in challenges)) .
endsWith("ge")));
=(a.substring (challenges. Challeng)) . endsWith("ge")));
= false challenges. Challeng does not end in ge)
So-> System . out . println( String . valueOf ( 68 + false ) ) ;
Ans. 68false
[1+1]
(h)Write an equivalent Java expression for the following :
b b24 ac
[2] 2a

**** double x = ( -b + Math . sqrt ( b * b 4 * a * c ) ) / 2 * a ,


y = ( -b + Math . sqrt ( b * b 4 * a * c ) ) / 2 * a ;
[1+1]
(i) Write a program fragment using if else statement which cannot be
replaced by a switch case statement or by one statement using a
ternary operator. [2]
**** if ( a >= 100 && a <= 9999 ) { digits = 3 ; valid = true ; }
// switch cannot copare range.
// One statement of ternary cannot assign two values.
[1+1]
(j) State why each of the following assignments result in compile-time
error-
(ii) long l = 6046531767 ; (ii) float f = 12.34 ;
(iii) char c = c ; c = c + 2 ; (iv) String str = 2 + 3 ;
[2]
**** (i) The value is of int data type, but exceeding the maximum value
of int.
(iii) The value is of double data type, but the variable is float.
(iv) The variable is char, but the expression returns an int value.
(v) The operands are int, so + acts as addition operator, but the
output is stored in a String object.
[+++]

SECTION B (60 Marks)


Attempt any four questions from this Section

Question 4.
Write a class having the name BackPack to purchase backpacks for kids
on-line. The class should have the following members:

13
Data members:
Instance variables :
String uName : To store the name of the buyer
long uMobNo : To store the mobile number of the buyer
int bagType : To store the type of bag chosen
int qty : To store the number bags to be purchased
String rName : To store the name of the recipient
String address : To store the address of the recipient
long rMobNo : To store the mobile number of the recipient
Class variables :
int rate1 , rate2 , rate3 : To store the rate of the three types of
bag
Member methods:
Backpack ( ) : Parameterised constructor to initialise the instance
variables with the accepted values
calcAmt ( ) : Calculates the payable amount.
The type and the price list of the bags is as follows:
Type Description Rate
1 Teddy soft toy bag ` 260
2 Bunny bag ` 399
3 Felt Velvet bag ` 499
display ( ) : Displays all the details and the total amount to be paid.
main ( ) : Create an object of the class and calls the above
methods. [15]

import java . util . Scanner ;


public class BackPack
{ String uName, rName , address ; long uMobNo , rMobNo ; int bagType ,
qty ;
static int rate1 = 260 , rate2 = 399 , rate3 = 499 , rate ; static long
amount ;

public BackPack ( String nam1 , long mobNo1 , int type , int qty , String
nam2 ,
String add , long mobNo2 )
{ uName = nam1 ; rName = nam2 ; address = add ;
uMobNo=mobNo1;rMobNo=mobNo2; this.qty=qty;
this.bagType=type; }

public void calcAmt ( )


{ switch ( bagType )
{case 1:rate=rate1;break; case 2:rate=rate2 break; case
3:rate=rate3; break ;
default :System.out.println("Wrong bag type option chosen."); rate
=0;}
amount = qty * rate ; }

14
public void display ( )
{ System . out . println ( "\nUser Name : " + uName ) ;
System . out . println ( "User Mobile number : " + uMobNo ) ;
System . out . println ( "\nType\tRate\tQuantity\tAmount" ) ;
System . out . println( bagType + "\t" + rate + "\t" + qty + "\t\t" +
amount ) ;
System . out . println ( "\nRecipient's Name : " + rName ) ;
System . out . println ( "Recipient's Mobile number : " + rMobNo ) ;
System . out . println ( "Delivery address : " + address ) ; }

public static void main ( String args [ ] )


{ Scanner scanS = new Scanner ( System . in ) ;
Scanner scanN = new Scanner ( System . in ) ;
String name1 , name2 , add ; long mob1 , mob2 ; int type , qty ;
System . out . print ( "Enter your name : " ) ; name1 = scanS .
nextLine ( ) ;
System .out.print("Enter your mobile number : ");mob1=scanN .
nextLong ( ) ;
System . out . print ( "Enter bag type : " ) ; type = scanN .
nextInt ( ) ;
System . out . print ( "Enter quantity : " ) ; qty = scanN . nextInt (
);
System . out . print ( "Enter recipient's name : " )
;name2=scanS.nextLine ( ) ;
System.out.print( "Enter recipient's mobile no. : ");mob2 =
scanN.nextLong() ;
System . out . print ( "Enter recipient's address : " ) ;add = scanS
.nextLine( ) ;
BackPack order = new BackPack
( name1 , mob1 , type , qty , name2 , add , mob2 ) ;
order . calcAmt ( ) ; order . display ( ) ; } }

Question 5.
Write a menu-driven program to display one of the following formats as
per the users choice after accepting the required details:

15
(iii) E.g. 1 Choice : 1 (iv) E.g. 2 Choice : 2
No. of lines : 6 Input word : Avatar
0 A
01 Av
024 Ava
0369 Avat
0481216 Avata
0510152025 Avatar
[15]

16
import java . util . Scanner ;
public class TwoDisplays
{ public static void main ( String args [ ] )
{ Scanner scan = new Scanner ( System . in ) ;
int choice , i , l ;
System . out . print ( "1. Display with numbers\n2. Display with a
word\n ..
Enter your choice - 1 or 2 : " ) ; choice = scan . nextInt ( ) ;
switch ( choice )
{ case 1 : System . out . print ( "Enter number of lines to be
displayed : " ) ;
lines = scan . nextInt ( ) ; int j ;
for ( i = 0 ; i < lines ; i ++ )
{ for ( j = 0 ; j <= i ; j ++ ) { System . out . print ( i *
j); }
System . out . println ( ) ; } break ;
case 2 : System . out . print ( "Enter a word : " ) ;
String word = scan . next ( ) , spaces = " " ; len = word .
length ( ) ;
for ( i = 1 ; i <= len ; i ++ )
{ spaces += " " ; }
for ( i = 1 ; i <= len ; i ++ )
{ System . out . println ( spaces + word . substring ( 0 ,
i));
spaces = spaces . substring (1) ; } break ;
default : System . out . println ( "Invalid choice." ) ; } }

Question 6.
Write a program in Java to store the name, country and the time taken to
complete the Rio de Janeiro Olympics marathon event by the following
two athletes in six variables. Calculate the average of the two
times clocked. Display all the details as shown below:
Description Name Country Time taken
Participant 1 Eliud Kipchoge Kenya 2:08:44
Participant 2 T. Gopi India 2:15:25
-------------------------------------------------------------------------------------------
Average Time _:__:__
------------------------------------------------------------------------------------------- [15]

public class MarathonAverage


{ public static void main ( String args [ ] )
{ String name1 = "Eliud Kipchoge" , name2 = "T. Gopi" , country1 =
"Kenya" ,
country2 = "India" , time1 = "2:08:44" , time2 = "2:15:25" ,
line = "--------------------------------------------------" ;

17
int index1 = time1 . indexOf ( ":" ) , lindex1 = time1 . lastIndexOf(":")
,
index2 = time2 . indexOf ( ":" ) , lindex2 = time2 . lastIndexOf(":")
,
hr1 = Integer . parseInt ( time1 . substring ( 0 , index1 ) ) ,
min1= Integer . parseInt ( time1 . substring ( index1 +1 , lindex1 )
),
sec1 = Integer . parseInt ( time1 . substring ( lindex1 + 1 ) ) ,
hr2 = Integer . parseInt ( time2 . substring ( 0 , index2 ) ) ,
min2 = Integer . parseInt ( time2 . substring ( time2 . index2 + 1 ,
time2 . lindex2 ) ) ,
sec2 = Integer . parseInt ( time2 . substring ( time2 . lindex2+
1)),
tothr = hr1 + hr2 , totmin = min1 + min2 ,
totsec = sec1 + sec2 + totmin * 60 + tothr * 60 * 60 ,
avgsec = totsec / 2 , avgmin = 0 , avghr = 0 ;
if ( avgsec >= 60 )
{ avgmin = avgsec / 60 ; avgsec %= 60 ; }
if ( avgmin >= 60 )
{ avghr = avgmin / 60 ; avgmin %= 60 ; }
System . out . println ( "Description\tName\t\tCountry\tTime Taken" ) ;
System . out . println ("Participant 1\t"+name1+"\t"+country1 +"\t"
+ time1 );
System . out . println ("Participant
2\t"+name2+"\t\t"+country2+"\t"+time2 );
System . out . println ( line + "\nAverage time\t\t\t\t" + avghr + ":" +
avgmin + ":" + avgsec + "\n" + line ); } }

Question 7.
Write a program in Java to accept 10 integers in an array. Delete the
duplicate elements. Display the array content before and after deletion.

Input : 2 , 33 , 9 , 78 , 10 , 45 , 9 , 48 , 10 , 2
Output : 2 , 33 , 9 , 78 , 10 , 45 , 48
[15]

import java . util . Scanner ;


public class DeleteDuplicateValues
{ public static void main( String args [ ] )
{ Scanner scan = new Scanner (System . in) ;
int arr [] = new int [ 10 ] , output [ ] = new int [ 10 ] , i , j , k ;

18
System . out . println ( "Enter 10 values : " ) ;
for ( i = k = 0 ; i < 10 ; i ++ )
{ System . out . print ( "Value no. " + ( i + 1 ) + " ~ " ) ;
arr [i] = scan . nextInt ( ) ; //Input
for ( j = 0 ; j < k ; j ++ ) // To check whether the accepted value is
{ // present in the output array
if ( arr [ i ] == output [ j ] ) break ;
// If present,not written in the output array
} // end of inner loop
if ( j == k ) // Condition will be true only if the accepted value
{ // is not present in the output array
output [ k ] = arr [ i ] ; k ++ ;
}
} // end of outer loop
System . out . print ( "\n\nInput : " ) ;
for ( i = 0 ; i < 10 ; i ++ )
{ System . out . print ( arr [ i ] + " " ) ; }
int newArr [ ] = new int [ k ] ;
/* To create an array with required number of elements
after deletion of the duplicate values */
for ( i = 0 ; i < k ; i ++ )
{ newArr [ i ] = output [ i ] ; }
arr = newArr ; /* Overwriting the address of the original array
stored in arr by the new array address*/
System . out . print ( "\n\nOutput : " ) ;
for ( i = 0 ; i < k ; i ++ )
{ System . out . print ( arr [ i ] + " " ) ; }
}
}

Question 8.
Using overloaded methods calculateSum ( ), write a program in Java to
calculate the sum of the following series:
(iii) s= 1 - 5 + 9 - 13 + 17 - 21
9 17 25 33 41
(iv) s = 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! +n terms
[15]

import java . util . Scanner ;


public class SumOfSeries
{ public static void main ( String args [ ] )

19
{ Scanner scan = new Scanner ( System . in ) ; int n , x ;
System . out . print ( "Enter the value of 'n' : " ) ; n = scan . nextInt
();
System . out . print ( "Enter the value of 'x' : " ) ; x = scan . nextInt
();
calculateSum ( ) ; calculateSum ( n , x ) ; }

public static void calculateSum ( )


{ //s = 1 - 5/9 + 9/17 - 13/25 + 17/33 - 21/41
int num , den , i ; float term , sum = 0 ; System . out . println ( "\n\n"
);
for ( num = den = i = 1 ; num <= 21 ; num +=4 , den += 8 , i ++ )
{ term = num / (float) den ;
if (i % 2 == 1) { if (num != 1)System . out . print (" + ") ; sum +=
term; }
else { sum -= term ; System . out . print ( " - " ) ; }
System.out.print( num + "/" + den ) ; } // end of loop
System . out . println ( " = " + sum ) ; } // end of method

public static void calculateSum ( int n , int x )


{ //s = (x+1) + (x+2)/2! + (2x+3)/3! + (3x+4)/4! +n terms
int num ; long fact ; double term , sum = 0 ;System . out . println
( "\n\n" ) ;
if ( n > 0 ) { System . out . println ( x + / + 1 ) ; sum = x / 1.0 ; }
for ( fact = 1 , num = 2 ; num + 1 <= n ; num ++ )
{ fact *= num ; term = ( ( num - 1 ) * x + num ) / (double) fact ;
System . out . print ( term ) ; if ( num != n ) System . out . print ( "
+");
sum += term ; } // end of loop
System . out . println ( " = " + sum ) ; } }

Question 9.
Using user-defined methods write a program in Java to accept a string and
sort the words in ascending order of alphabets.

20
Input : If you litter, The future will be bitter
Output : If The be bitter future litter, will you
[15]

import java . util . Scanner ;


public class SortWordsOfAString
{ public static void main ( String args [ ] )
{ Scanner scan = new Scanner ( System . in ) ;
String str ;
System . out . print ( "Enter a string : " ) ; str = scan . nextLine ( ) ;
System . out . println ( "\n\nInput : " + str ) ;
String words [ ] = splitWords ( str ) ; //To store one word in one
element
sortWords ( words ) ; } // main

public static String [ ] splitWords ( String str )


{ int sLen = str . length ( ) ;
String words [ ] = new String [ sLen ] ; int i , j , index ;
for ( i = j = 0 ; i < sLen ; i = index + 1 , j ++ )
{ index = str . indexOf ( ' ' , i ) ; //Searching for next space in the
string
if ( index == -1 ) index = sLen ;//helps for the last word
words [ j ] = str . substring ( i , index ) ;}//copying one word in an
element
/* Alternate method of splitting the word
Scanner wo = new Scanner ( str ) ; j = 0 ;
while ( wo . hasNext ( ) ) { words [ j ] = wo . next ( ) ; j ++ ; } */
String w [ ] = new String [ j ] ; //creating an array with length of no. of
words
for ( i = 0 ; i < j ; i ++ )
{ w [ i ] = words [ i ] ; }
return w ; } // splitWords

public static void sortWords ( String words [ ] )


{ int i , j , len = words . length ; String s , output = ;
for ( i = 0 ; i < len ; i ++ )
{ for ( j = 0 ; j < len - 1 - i ; j ++ )
{ if ( words [ j ] . compareTo ( words [ j + 1 ] ) > 0 )
{ s = words [ j ] ; words[ j ] = words[ j + 1 ] ; words[ j + 1 ] =
s ;}} }
for ( i = 0 ; i < len ; i ++ ) { output += words [ i ] + ; }
output = output . trim ( ) ; System . out . print ("Output : " +
output ) ;} }

21

BLUE PRINT

NEW HORIZON PUBLIC SCHOOL


INDIRANAGAR, BANGALORE 560 008.
FIRST TERMINAL EXAMINATION SEPTEMBER 2016
COMPUTER APPLICATIONS
STD. : X MAX MARKS :
100
DATE : 23/09/2016 TIME : 2 HOURS

QUESTION COMPREHENSIO
RECALL APPLICATION TOTAL
NO N

1 4 6 - 10

2 2 7 1 10

3 10 - 10 20

4 - - 15 15

5 - - 15 15

6 - - 15 15

7 - - 15 15

8 - - 15 15

9 - - 15 15

TOTAL 16 13 101 130

QUESTION PAPER SET BY :

Madhumita K. [ X B , X D ]

OTHER TEACHERS :
Radhika O. [ X C ]

Srividya R.. [ X E ]

22

23

You might also like