Computer Applications Answer Key Class IX December 2017 Question 1. a) What are the default values of the primitive data type int and float? Ans : int = 0 and float 0.0f; b) Name any two OOP s principle. Ans : Abstraction, Encapsulation, polymorphism, class, object and inheritance c) What are identifiers? Identifiers are the user defined name for the various parts of the program d) Identify the literals listed below: i) 0.5 floating point ii) A character iii) false Boolean iv) a String e) Define the term bytecode. Bytecode is the compiled code that can be interpreted by JVM. Question 2. a) Evaluate the value of n if value of p=5, q=19 int n= (q-p)>(p-q)? (q-p) : ( p-q); 14 b) Arrange the following primitive data types in ascending order of their size: i) char ii) byte iii) double iv) int byte< char< int < double c) What is the value stored in variable res given below: double res= Math.pow( 345.indexOf( 5 ),3); Ans : 8 b) What is the result stored in x, after evaluating the following expression int x=5; x=x++ * 2 + 3 * --x 25 e) What are the values of a and b after the following function is executed, ifthe values passed are 30 and 50: void paws(int a, int b) a=a+b; b=a-b; a=a-b; System.out.println(a+, +b); Ans : 50 30 Question 3. a) State the data type and value of y after the following is executed: char x= 7 ; y=character.isletter(x); Ans : boolean, false b) What will be the output of the following code? int m=2; int n=15; for(int i=1;i<=15;i++); m++;
--n; System.out.println("m="+m); System.out.println("n="+n); 3 14 c) State the output when the following program segment is executed: String a= Smartphone, b= Graphic Art ; String h=a.substring(2,5); String k=b.substring(8).touppercase( ); System.out.println(h); System.out.println(k.equalsIgnoreCase(h)); Ans : art true d) The access specifier that gives the most accessibility is and the least accessibility is. Ans : public & private f) i) Name the mathematical function which is used to find sine of an angle given in radians. Math.sin() ii) Name a string function which removes the blank spaces provided in the prefix and suffix of a string. trim() f) i) What will this code print? int arr[]= Computer Applications System.out.println(arr.charAt(22)); Ans : Error ii) Name the keyword which is used to resolve the conflict between method parameter and instance variables/fields. Ans : this g) What will the following functions return when executed? i) Math.max(-17,-19) ==== -17 ii) Math.ceil(7.8) ===== 8.0 h) Write the output of the following program code: char ch; int x= 97; do ch=(char)x; System.out.print(ch + ); if(x%10 = =0) ++x; while(x<=100); Ans : a b c d i) Write the Java expressions for: a 2 +b 2 -------- 2ab Ans : (Math.pow(a,2)+Math.pow(b,2))/(2*a*b)
j) If int y=10 then find int z=(++y * (y++ + 5)); Ans : z = 176 Question 4. [ 15 ] Write a program to Accept a number and Check and print whether it is a palindrome number Example Input1 : 1221 Output : 1221 is a palindrome number Input2 : 122 Output : 122 is not a palindrome number public class IXQ4 System.out.println("enter a number "); int n = sc.nextint(); int t = n; // storing the n umber in a temporary variable int rev=0; // to store the reversed number while(t!=0) int d = t%10; rev = rev*10 + d; t /= 10; if (rev == n) System.out.println(n+" is a palindrome number"); System.out.println(n+" is not a palindrome number"); Question 5. Write a program to input a number and print whether the number is a special number or not. (A number is said to be a special number, if the sum of the factorial of the digits of the number is same as the original number). Example: 145 is a special number, because 1! + 4! + 5! = 1 + 24 + 120 = 145 (where! stands for the factorial of the number and the factorial value of a number is the product of all integers from 1 to that number, example 5! = 1*2*3*4*5 = 120).
public class IXQ5 System.out.println("enter a number "); int n = sc.nextint(); int t = n; // storing the n umber in a temporary variable int sum=0; // to store the reversed number while(t!=0) int d = t%10; int f =1; for(int i = 1; i<=d; i++) f *= i; sum +=f; t /= 10; if (sum == n) System.out.println(n+" is a Special Number"); System.out.println(n+" is not a Special number"); Question 6. [ 15 ] Write a program to input and store roll number, name and marks in 3 subjects of a student and display the remark based on average mark as given below: (The maximum marks in the subject are 100) Average marks = Total marks ------------------ 3 Average marks Remark 85-100 EXCELLENT 75-84 DISTINCTION 60-74 FIRST CLASS 40-59 PASS Less than 40 POOR Ans : public class IXQ6
System.out.print("Enter yout name : "); String name = sc.nextline(); System.out.print("Enter yout RollNumber : "); int roll = sc.nextint(); String st = sc.nextline(); // to capture the enter key System.out.println("Enter the three subject marks"); System.out.print("Enter Marks 1: ") ; int m1 = sc.nextint(); st = sc.nextline(); System.out.print("Enter Marks 2: ") ; int m2 = sc.nextint(); st = sc.nextline(); System.out.print("Enter Marks 3: ") ; int m3 = sc.nextint(); st = sc.nextline(); int Avg = (m1+m2+m3)/3; String rem=""; if (Avg >=85 && Avg <=100) rem = "EXCELLENT"; if(avg >=75 && Avg<=84) rem = "DISTINCTION"; if (Avg>=60 && Avg<=74) rem = "FIRST CLASS"; if (Avg>=40 && Avg <=59) rem = "PASS"; rem = "POOR"; System.out.println("Roll \t Name \t Average \t Remarks"); System.out.println(roll +" \t "+ name +" \t "+ Avg+ " \t "+ rem); Question 7. Write a program to accept a number and check and display whether it is a prime number or not and if it is a prime number print its twin prime also if it does not have any print appropriate message. Twin prime numbers : a twin prime numbers are prime numbers and the difference between the two numbers is 2. Example 17 is a prime number and its twin prime is 19. 2 is a prime number but 4 is not a prime number so 2 does not have twin prime. class IXQ7
System.out.println("Enter a number that is Prime"); int num = sc.nextint(); int i; for(i = 2; i<num ;i++) if(num%i ==0) if(i == num) int t = num+2; for(i = 2; i<t ;i++) if(t%i ==0) if (i == t) System.out.println(num+" & "+t+" are twin prime numbers"); t = num-2; for(i = 2; i<t ;i++) if(t%i ==0) if (i == t) System.out.println(t+" & "+num+" are twin prime numbers"); System.out.println(num+" does not have twin prime"); System.out.println(num +" is not a prime number"); Question 8:
Write a program to input a string in uppercase and print the frequency of each character. Example: INPUT: COMPUTER HARDWARE OUTPUT: CHARACTERS FREQUENCY A 2 C 1 D 1 E 2 H 1 M 1 O 1 P 1 R 3 T 1 U 1 W 1 public class IXQ8 // instance variables - replace the example below with your own System.out.println("Enter a String "); String str = sc.nextline(); str = str.touppercase(); System.out.println("CHARACTER FREQUENCY"); for(char ch = 'A'; ch<='z';ch++) int count =0; for(int i = 0 ;i<str.length();i++) if(str.charat(i) == ch) count++; if (count!= 0) System.out.println("\t"+ch + "\t" + count); Question 9. [ 15 ]
Using the switch write a menu driven program to : i) To find and display all the factors of a number input by the user (including 1 and excluding the number itself). Example: Sample input : n = 15 Sample output : 1, 3, 5 ii) To find and display the factorial of a number input by the user ( the factorial of a non negative integer n, denoted by n!, is the product of all integers less than or equal to n. Example: Sample input : n =5 Sample output : 5!=1x2x3x4x5=120 For an incorrect choice an appropriate error message should be displayed. public class IXQ9 System.out.println("1. Display factors of a number"); System.out.println("2. Diaplay factorial of a number"); System.out.println("3. Exit"); int choice = sc.nextint(); int num; switch (choice) case 1: System.out.println("enter the number "); num = sc.nextint(); System.out.print("The factors of "+num+" are "); for(int i= 1; i<num; i++) if (num%i == 0) System.out.print(i+" "); case 2 : System.out.println("enter the number "); num = sc.nextint(); int fact = 1; for(int i = 1;i<=num;i++) fact *= i; System.out.println("the factorial of "+num+" is "+fact);
case 3: System.out.println("you have chose to exit: Thank you"); default: System.out.println("INVALID CHOICE");