0% found this document useful (0 votes)
21 views5 pages

100 Mks Test std-9-1 - Answers

The document contains a series of Java programming questions and exercises, including multiple-choice questions, coding tasks, and class design requirements. It covers topics such as data types, control structures, object-oriented programming principles, and error handling. The document is structured into two sections, with Section A focusing on theoretical questions and Section B requiring practical coding solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

100 Mks Test std-9-1 - Answers

The document contains a series of Java programming questions and exercises, including multiple-choice questions, coding tasks, and class design requirements. It covers topics such as data types, control structures, object-oriented programming principles, and error handling. The document is structured into two sections, with Section A focusing on theoretical questions and Section B requiring practical coding solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

STD-9 FM:-100

SECTION A (40 Marks)


(Attempt all questions from this Section)
Question 1[20]
Choose the correct answers to the questions from the given options. (Do not copy the question, write the correct answer only.)
(i) Name the feature of Java depicted in the following picture((a) Data abstraction,(b) Polymorphism,(c)Inheritance,
(d)Encapsulation)

(ii) Unicode character coding scheme uses _________ integer literals.((a) Decimal,(b) Octal,(c) Hexadecimal,(d) All of these)
(iii) The constant that reserves 64 bits of memory space is-((a) "HUMAN",(b) 23.56,(c) 103,(d) G)
(iv) Unique ID of students is an example of-((a) Data type,(b) Characteristic,(c) Behaviour,(d) Keyword)
(v) The keyword else is associated with:((a) Single if-statement,(b) Multiple if-statement,(c)for loop,(d)None of these)
(vi)Which of the following can be a return value of nextFloat() method?((a)6.2,(b)1.0F,(c)40,(d)All of these
(vii) Which of the following is/are illegal lines of Java code ?1. string str "CODING CONCEPT 2. int byte1795((a) Only 1,(b) Only 2,
(c) Both 1 and 2,(d) None of these)
(viii) The method that accepts a string without any space is:((a) nextSpace(),(b) next(),(c) nextLine(),(d)nextBoolean())
(ix) The number-423 can be stored in a variable of _____ data type.((a)short,(b)int,(c)long(d)All of these)
(x) Predict the output, if n=500.
int n, a= 500;
if(a+n*10>5000)
System.out.println(100);
else
System.out.println(200),
((a) 100,(b) 200,(c) No output will be produced,(d) Syntax error)
(xi) Which of the following is not a Java reserved word?((a) import,(b) public,(c) class,(d) None of these)
(xii) The term used to express the ability of objects to interact with each other is:((a)instantiation,(b)Message passing ,(c)logical
construct,(d)all of these)
(xiii)Assertion(A)Math.sqrt() method cannot be used to find the square root of negative number.
Reason(R)The method returns NaN for a negative number.((a)Both A and R are true and R is the correct explanation of A,(b)Both
A and R are true and R is not the correct explanation of A,(c)A is false and R is true,(d)A is true and R is false)
(xiv)The operation that is at first performed by any loop is:((a)initialisation of loop control variable,(b)checking loop condition,
(c)updation of loop control variable,(d)calling a function)
(xv) The error that is encountered during the compile-time of the program is _____ error.((a)syntax (b)logical (c)runtime
(d)exception)
(xvi)What will be the final value that will be stored in the variable res?double res=Math.abs(Math.floor(-8.50))((a)-8.0,(b)-9,
(c)9.0,(d)9)
(xvii)The data type that is not supported for a case value in switch-case statement is:((a)long (b)char (c)boolean (d)double)
(xviii)An operator that is used to form a condition is:((a)+= (b)* (c)== (d)=
(xix) Assertion (A) The for loop is an entry-controlled loop.
Reason (R)The value of the loop control variable is always increased in its every iteration
(a) Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
(b) Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A)
(c) Assertion (A) is false and Reason (R) is true (d) Assertion (A) is true and Reason (R) is false
(xx) Predict the output of the following code segment:
int d = 10;System.out.println(d%d/d);((a) 10 (b)1 (c)0 (d)11)
Question 2 [20]


8
(i) Write java expression for: ax 3 + by double r=Math.sqrt(a*x*x*x+(b*Math.pow(y,8))/(c*x-y);
cx− y

(ii) Find the output of:


class test
{public static void main()
{int i=2,k=1;
while (++i<6)k *= i;System.out.println(k);}} 60
(iii)Give the output of the following code fragment when i) cat=’g’ -Gold ii) opn = ‘n’-error
switch(cat)
{case ‘g’:System.out.println(“Gold”);break;
case ‘s’:System.out.println(“Silver”);
case ‘b’:System.out.println(“Bronze”);break;
default:System.out.println(“Platinum”);}
(iv)Find the output of the following program segment:
int m=15, n=10;
for(int i=1;i<=5;++i);m++; - -n;System.out.println(m+” “+n); 16 9
(v) Find the output of the following program segment:
int m,n=400;m= n>=400? –n : n++;
System.out.println(“output = “+m); output=399
(vi) Write the correct identifiers out of the following: While, Switch, 2ndName, A%B
(vii) What is the default value of char and Boolean?false
(viii)Name the principles of OOP.-encapsulation,inheritance,polymorphism
(ix)Rewrite into if-else int max=((a>b)&&(a>c))?a:((b>a)&&(b>c))?b:c;
if((a>b)&&(a>c))
max=a;
else if((b>a)&&(b>c))
max=b;
else
max=c;
(x) Write an infinite loop in for and while.for( ; ;) while(true){ }

SECTION B (60 Marks)

(Answer any four questions from this Section)

Question 3
Design a class for a menu-driven program using switch-case for the following purposes in accordance with
a choice entered by the user:[15](i) Accept a number and print its total number of factors.
(ii) Accept a number and print its sum of all factors. Display a relevant error message if the choice is anything else.
import java.util.*;
class menu
{public static void main()
{Scanner br=new Scanner(System.in);
int ch,n,i,c=0,s=0;
System.out.println(“Enter choice”);
ch=br.nextInt();
switch(ch)
{case 1:System.out.println(“Enter a no.”);
n=br.nextInt();
for(i=1;i<=n;i++)
{if(n%i==0)
c++;}
System.out.println(c);
break;
case 2:System.out.println(“Enter a no.”);
n=br.nextInt();
for(i=1;i<=n;i++)
{if(n%i==0)
s=s+i;}
System.out.println(s);
break;
default:System.out.println(“Wrong input”);}}}
Question 4
A library charges a fine for books returned late as per the criteria given below:-
Number of days Charge
First 5 days Rs.2 per day
Next 5 days Rs.5 per day
Next 10 days Rs.7 per day
Above 20 days Rs.10 per day
Design a class to accept the full name of the borrower and the number of days late to return the book and compute the amount
of fine to be paid. Thereafter, display the results in the following format :-
Name of the borrower
Number of days late to return the book
Amount of fine to be paid
Execute the program for ‘N’ number of customers.
import java.util.*;
class menu
{public static void main()
{Scanner br=new Scanner(System.in);
int d,i,N;String na;int a=0;
System.out.println(“Enter no. of customers”);
N=br.nextInt();
for(i=1;i<=N;i++)
{System.out.println(“Enter customer name and number of days”);
na=br.nextLine();d=br.nextInt();
if((d>=1)&&(d<=5))
a=d*2;
else if((d>=6)&&(d<=10))
a=(5*2)+(d-5)*5;
else if((d>=11)&&(d<=20))
a=(5*2)+(5*5)+(d-10)*7;
else
a=(5*2)+(5*5)+(10*7)+(d-20)*10;
System.out.println(na+” “+d+” “+a);}}}
Question 5
Design a class for a menu-driven program using if-else with else if ladder for the following purposes in accordance with a choice
entered by the user.
(1) Print pattern 1-1*3*5 (2) Print pattern 2-#***# (3) Print pattern 3-23456
Display a relevant error message if the user opts for an invalid choice.
import java.util.*;
class menu
{public static void main()
{Scanner br=new Scanner(System.in);
int ch,i;
System.out.println(“Enter choice”);
ch=br.nextInt();
switch(ch)
{case 1:
for(i=1;i<=5;i++)
{if(i%2!=0)
System.out.print(i);
else
System.out.print(“*”);}
break;
case 2: for(i=1;i<=5;i++)
{if((i==1)||(i==5))
System.out.print(“#”);
else
System.out.print(“*”);}
break;
case 3: for(i=2;i<=6;i++)
{
System.out.print(i);}
break;
default:System.out.println(“Wrong input”);}}}

Question 6 WAP to input a number, check and print if it’s sapphire number. A sapphire number is any number which is equal to
n3-1.For eg-23-1=7 so 7 is sapphire number.
import java.util.*;
class pronic
{ public static void main()
{ Scanner br=new Scanner(System.in);
int n,i;
System.out.println("Enter a number");
n=br.nextInt();
for(i=1;i<n;i++)
{if(((i*i*i)-1)==n)
{System.out.println(n+"Sapphire number");
System.exit(0); } }
System.out.println(“Not Sapphire number”); }}
Question 7 WAP to input a number, check and print if it’s a Sparse number.A sparse number has number of 0’s more than half of
count of digits.For eg-1000 (no.of 0’s is 3 which is greater than 2)
import java.util.*;
class sparse
{ public static void main()
{ Scanner br=new Scanner(System.in);
int n,n1,d=0,c=0,s=0;
System.out.println("Enter a number");
n=br.nextInt();
n1=n;
while(n!=0)
{d=n%10;
c++;
if(d==0)s++;
n=n/10;}
if(s>(c/2))
System.out.println(n1+”Sparse Number”);
else
System.out.println(n1+”Not Sparse Number”);}}
Question 8 Write a menu driven program to do the following:-
‘1’-to print the first 10 terms of fibbonacci series.
1 2 3
+
‘2’-to print the series- a a a …p terms
1
+ 2 + 3 +¿
a a a
For any other input print wrong choice.
import java.util.*;
class menu
{public static void main()
{Scanner br=new Scanner(System.in);
int ch,i,a,b,c=0,p;
System.out.println(“Enter choice”);
ch=br.nextInt();
switch(ch)
{case 1:a=0;b=1;double s=0.0d;
System.out.print(a);
System.out.print(b);
for(i=3;i<=10;i++)
{c=a+b;System.out.print(c);
a=b;b=c;}
break;
case 2:
System.out.println(“Enter value of a and p”);
a=br.nextInt();p=br.nextInt();
for(i=1;<=p;i++)
{s=s+(1.0/a)/Math.pow(a,i);}
System.out.println(s);
break;
default:System.out.println(“Wrong Choice”);
}}}

You might also like