Formatting page ...
Question 1. (a) Define Encapsulation. [2] answer : Binding up of data and associated functions together into a single unit called (class) is called Encapsulation. (b) What are keywords? Give an example. [2] answer : These are the special reserved words which have some specific meaning to the Java interpreter. Examples: break, import, int etc. (c) Name any two library packages. [2] answer : java.io, java.util (d) Name the type of error (syntax, runtime or logical error) in each case given below: [2] (i) Math.sqrt (36-45) (ii) int a;b;c; answer : (i) runtime error (ii) syntax error (e) If int x [ ] = { 4, 3, 7, 8, 9, 10 }; what are the values of p and q? [2] (i) p = x.length (ii) q = x[2] + x[5] * x[1] answer : (i) 6 (ii) 37 Question 2. (a) State the difference between = = operator and equals( ) method. [2] answer : = = operator equals( ) 1. It is a relational operator 1. It is a String function 2. It is used to check for equality of any primitive data types. 2. It is used to check for equality of two String objects. (b) What are the types of casting shown by the following examples: [2] (i) char c = (char)120; (ii) int x = t ; answer : (i) explicit (ii) implicit (c) Differentiate between formal parameter and actual parameter. [2] answer : formal actual 1. parameters which are specified in the function prototype while creating a function. 1. parameters which are specified while calling a function. 2. contains data-type in their declaration. 2. does not contain data-type. (d) Write a function prototype of the following: [2] A function PosChar which takes a string argument and a character argument and returns an integer value. answer : int PosChar(String s, char c) (e) Name any two types of access specifiers. [2] answer : public and private Question 3. (a) Give the output of the following string functions : [2] (i) MISSISSIPPI .indexOf( S ) + MISSISSIPPI .lastIndexOf( I ) (ii) CABLE .compareTo( CADET ) answer : (i) 12 (ii) -2 (b) Give the output of the following Math functions: [2] (i) Math.ceil(4.2) (ii) Math.abs(-4) answer : (i) 5.0 (ii) 4 (c) What is a parameterized constructor? [2] answer : A parameterized constructor is a member method of a class which has the same name as that of the class and it initializes the instance variables of the class with its parameters. Example: If class declaration is class Sample { int a, b; . . } then Sample(int aa, int bb) is a parameterized constructor. (d) Write down java expression for : [2] T= answer : double T = Math.sqrt((Math.pow(A,2) + Math.pow(B,2) + Math.pow(C,2)) or double T = Math.sqrt((A*A + B*B + C*C)) (e) Rewrite the following using ternary operator : [2] if(x%2 == 0) System.out.print( EVEN ); else System.out.print( ODD ); answer : System.out.print((x%2==0? EVEN : ODD )); (f) Convert the following while loop to the corresponding for loop : [2] int m = 5, n = 10; while (n>=1) { System.out.println(m*n); n ; } answer : int m = 5, n; for(n=10; n>=1; n ) { System.out.println(m*n); } or int m = 5, n = 10; for(; n>=1; n ) { System.out.println(m*n); } (g) Write one difference between primitive data types and composite data types. [2] answer : primitive data types are built-in data types provided by Java whereas, composite datatypes are datatypes that are based on fundamental or primitive datatypes. (h) Analyze the given program segment and answer the following questions : [2] (i) Write the output of the program segment (ii) How many times does the body of the loop gets executed? for(int m=5; m<=20; m+=5) { if(m%3 == 0) break; else if(m%5 == 0) System.out.println(m); continue; } answer : (i) 5 10 (ii) 3 times Working : When m = 5, if(m%3 == 0) is false if(m%5 == 0) is true so it prints 5 Loop executed 1 time. When m = 10, if(m%3 == 0) is false if(m%5 == 0) is true so it prints 10 Loop executed 2 times. When m = 15, if(m%3 == 0) is true so, break is encountered and loop terminates. Loop executed 3 times. (i) Give the output of the following expression : [2] a+=a++ + ++a + a + a ; when a = 7 answer : 39 Working : a+=a++ + ++a + a + a ; a = a + a++ + ++a + a + a ; a=7+7+9+8+8 a = 39 (j) Write the return type of the following library functions : [2] (i) isLetterOrDigit(char) (ii) replace(char,char) answer : (i) boolean (ii) String
|