Good Earth School Naduveerapattu Date:.2.207 Marks: 70 Class: XI Second Term Examination Computer Science Answer Key Time: 3 hrs. Candidates are allowed additional 5 minutes for only reading the paper. They must NOT start writing during this time. All working, including rough work should be done on the same sheet as the rest of the answer. The intended marks for questions or parts of questions are given in brackets ( ). Part I Answer all the questions Question. [5] a) Using truth table, verify the following expression: X + ( Y + Z ) = ( X + Y ) + Z Also state the law. X Y Z Y+Z X+(Y+Z) X+Y (X+Y)+Z 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Associative Law b) Given, F(X, Y, Z)= (X + Y ). (Y + Z ) write the function in canonical product of sum form. = (X + Y + 0). (Y + Z + 0) = (X +Y +(Z.Z ).( Y + Z +(X.X ) =(X +Y +Z).(X +Y +Z ).(Y+Z +X).(Y+Z +X ) c) Draw the truth table and logic circuit for a 2 input XNOR gate. A B X X = AB+A B 0 0 0 0 A B 0 0 or A A B B d) Find the complement of the following expression: =X.(X +Y) e) If (X Y) then write its: i) Converse Y X ii) Contra positive ~Y ~X
Question 2. [0] a) A square matrix M[][] of size 0 is stored in the memory with each element requiring 4 bytes of storage. If the base address at M[0][0] is 840, determine the address at M[4][8] when the matrix is stored in row major wise. Row Major address formula : M[i][j] = B + W [(i lr) * column + (j lc)] B = 840, lr = 0, lc = 0, W = 4, rows = 0, column = 0, i = 4, j = 8 M[4][8] = 840 + 4 [ ( 4-0 ) x 0 + ( 8 0 ) ] = 840 + 92 = 2032 b) Name the file stream classes to : i) Write data to a file in binary form. FileOutputStream / DataOutputStream/ FileWriter/ OutputStream ii) Read data from a file in text form. FileReader / DataInputStream/ InputStream/ FileInputStream c) What is a constructor? State one difference between a constructor and member function of a class. A constructor is a member function which has the same name as its class and it gets invoked every time a new object is created, it is used to construct memory and initialize object values. Constructor Member function of a class Constructor do not return a value not even void Member function of a class should return a value or declared as void Invoked automatically Has to be called Same name as that of a class Has a different name d) State the use of exception handling. Name the two types of exceptions. Exception handling is a transparent and neat way for handling program errors Types of exceptions checked and unchecked exceptions. e) Manipulate the following binary numbers i. (000) 2 + (000) 2 000 000 ------------ 0 (000) 2 + (000) 2 = (0) 2 ii. (0000) 2 - (00) 2 0000 --- 00 --- 2 s complement of 2 0 --- 2 Now add to 0000 --- 0 --- 2 ---------- 000
2s complement 000 (0000) 2 - (00) 2 = (0) 2 iii. (.) 2 = (7.75) 0 iv. (00 0 00 00) 2 = (622) 8 Question 3. [5] a) The following function is a part of some class which computes and sorts an array arr[] in ascending order using the bubble sort technique. There are some places in the code marked by??,?2?,?3?,?4?,?5?, which must be replaced by a statement /an expression so that the function works properly: class Trial void bubblesort(int arr[]) inti,j,k,tmp; for(i=0;??;i++) for(j=0;?2?;j++) if(arr[j]>?3?) tmp=arr[j];?4?=arr[j+]; arr[j+]=?5?; i) What is the expression or statement at?? i<arr.length- or i<size- or i <k- (where k = arr.length) ii) What is the expression or statement at?2? j<arr.length- or j<arr.length--i or j < k- iii) What is the expression or statement at?3? arr[j+] iv) What is the expression or statement at?4? arr[j] v) What is the expression or statement at?5? tmp Part II Answer seven questions in this part, choosing two questions from Section A, two from Section B and two from Section C. Question 4 Section A Answer any two questions a) Given the Boolean function F(A,B,C,D)= Σ(4,6,7,0,,2,4,5)
i) Reduce the above expression by using 4 variable K- map, showing the various groups ( i.e. octets, quads and pairs). ii) Draw the logic gate diagram of the reduced expression. Assume that the variables and their complements are available as inputs. [4 + = 5] CD AB C D C D CD CD A B 0 3 2 A B 4 5 7 6 AB 2 3 5 4 AB 8 9 Quad :BD Quad 2 :BC Quad 3 :AC The reduced Expression is => BD +BC+AC A B C D 0 BD BC AC b) Given the Boolean function F(P,Q,R,S)= Π(0,5,7,8,0,2,3,4,5) i) Reduce the above expression by using 4 variable K- map, showing the various groups ( i.e. octets, quads and pairs). ii) Draw the logic gate diagram of the reduced expression. Assume that the variables and their complements are available as inputs. [4 + = 5] R+S P+Q R+S R+S R +S R +S P+Q 0 0 3 2 P+Q 4 0 5 0 7 6 P +Q 0 2 0 3 0 5 0 4 P +Q 0 8 9 0 0
Pair : Q+R+S Pair 2 : P +R +S Quad : Q +S Quad 2 : P +Q P Q R S Q+R+S P +R +S Q +S P +Q Question 5. The principal of a school intends to select students for admission to Class XI on the following criteria: Student is of the same school and has passed the Class X Board Examination with more than 60% marks. OR Student is of the same school, has passed the Class X Board Examination with less than 60% marks but has taken active part in co-curricular activities. OR Student is not from the same school but has either passed the Class X board Examination with more than 60% marks or has participated in Sports at the national level. The inputs are : INPUTS S - Student is of the same school P - Has passed the Class X Board Examination with more than 60% marks. C - Has taken active part in co-curricular activities. T - Has participated in sports at the National Level. OUTPUT:- X Denotes admission status [ indicates granted and 0 indicates refused in all the cases.] a) Draw the truth table for the inputs and outputs given above and write the SOP expression. [3] b) Reduce X(S,P,C,T) using Karnaugh s map. [5] Draw the logic gate diagram for the reduced SOP expression for X(S,P,C,T) using AND and OR gate. You may use gates with two or more inputs. Assume that the variable and their complements are available as inputs. [2] S P C T X 0 0 0 0 0 0 0 S P C T 0 0 0 0 0 S P CT 0 0 0 S PC T
0 0 S PC T 0 0 S PCT 0 S PCT 0 0 0 0 0 0 0 SP CT 0 SP CT 0 0 SPC T 0 SPC T 0 SPCT SPCT X(S,P,C,T) = S P C T + S P CT + S PC T + S PC T + S PCT + S PCT + SP CT + SP CT+ SPC T + SPC T + SPCT + SPCT CT SP C T C T CT CT S P 0 3 2 S P 4 5 7 6 SP 2 3 5 4 SP 8 9 0 Quad : S T Quad 2 : SC Octet : P X(S,P,C,T) = S T+SC+P S P C T Question 6 a) Verify algebraically if, Taking LHS ( ) ( ) ( ) ( ) ( ) ( ) Z+Z = [2] ( ) ( ) ( ) X +Y => RHS b) Represent the Boolean expression X + YZ with the help of NOR gates only. [2]
X Y Z c) Define the terms contingency, contradiction and tautology. [3] Contingency: The propositions that have some combination of s and 0s in their truth table output column. Contradictions : The propositions that have all 0s in their truth table output column Tautology : The propositions that have all s in their truth table in output column [3] d) Consider the following truth table where A and B are two inputs and X is the output: A B X 0 0 0 0 0 0 i) Name and draw the logic gate for the given truth table. A B ii) Write the POS of X(A,B) X(A,B) = (A+B)(A +B ) [2] [] SECTION B Answer any two Question 7 Design a class VowelWord to accept a sentence and calculate the frequency of words that begin with a vowel. The words in the input string are separated by a single blank space and terminated by a full stop. The description of the class is given below: Class Name : VowelWord Data members str : to store a sentence freq : to store the frequency of words beginning with a vowel. Member functions VowelWord() : constructor to initialize data members to legal initial values. void readstr() : to accept a sentence. void freq_vowel( ) : counts the frequency of the words beginning with a vowel.
void display() : to display the original string and the frequency of the words that begin with a vowel. Specify the class VowelWord giving details of the constructor( ), void readstr(), void freq_vowel() and void display(). Also define the main function to create an object and call the methods accordingly to enable the task. [0] import java.io.*; public class VowelWord String str ; int freq; VowelWord() //: constructor to initialize data members to legal initial values. str = ""; freq = 0; void readstr() throws IOException //: to accept a sentence. BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a sentence:"); str = br.readline(); void freq_vowel( ) // : counts the frequency of the words beginning with a vowel. if ("aeiouaeiou".indexof(str.charat(0)) >=0) freq++; for(int i=; i<str.length(); i++) if ((str.charat(i) == ' ') &&("aeiouaeiou".indexof(str.charat(i+)) >=0)) freq++; void display() System.out.println("the original String is : "+ str); System.out.println("the number of words that start with a vowel is : "+freq); public static void main(string args[])throws IOException VowelWord v = new VowelWord(); v.readstr(); v.freq_vowel(); v.display();
Question 8. A class Collection contains an array of 00 integers. Using the following class description create an array with common elements from two integer arrays. Some of the members of the class are given below: Class name : Collection Data members arr[] : integer array len : length of the array Member functions Collection() : default constructor Collection(int) : parameterized constructor to assign the length of thearray. voidinparr() : to accept the array elements. Collection common(collection) : returns a Collection containing the common elements of current Collection object and the Collection object passed as a parameter. void arrange() : sort the array elements of the object containing common elements in ascending order using any sorting technique. void display() : displays the array elements. Specify the class Collection giving the details of the constructors, void inparr() and void arrange(). Collection common(collection). You need not write the main Function. [0] import java.io.*; public class Collection int arr[]; int len; Collection() len =0; //: default constructor Collection(int len) thearray. this.len = len; arr = new int[len]; // : parameterized constructor to assign the length of void inparr()throws IOException //: to accept the array elements. BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the values of the array"); for(int i = 0; i <len; i++) arr[i] = Integer.parseInt(br.readLine());
Collection common(collection c) //: returns a Collection containing the common elements //of current Collection object and the Collection object // passed as a parameter. int a = c.arr.length; int b = this.arr.length; int k = Math.max(a,b); int l=0; Collection result = new Collection(Math.min(a,b)); for(int i=0; i<k; i++) for(int j=0; j<k; j++) try if(c.arr[i] == this.arr[j]) result.arr[l++]= c.arr[i]; catch (Exception e) ; return result; void arrange() //: sort the array elements of the object containing //common elements in ascending order using any sorting technique. for(int i = 0; i<arr.length; i++) for(int j = 0; j<arr.length--i; j++) try if (arr[j] > arr[j+]) int t = arr[j]; arr[j] = arr[j+]; arr[j+] = t; catch(exception e) ;
Question 9. A happy number is a number in which the eventual sum of the square of the digits of the number is equal to Example : 28 = (2) 2 + ( 8 ) 2 = 4 + 64 = 68 68 = (6) 2 + ( 8) 2 = 36 + 64 = 00 00 = ( ) 2 + ( 0 ) 2 + ( 0 ) 2 = + 0 + 0 = Hence 28 is a happy number. Example : 2 = () 2 + (2) 2 = + 4 = 5 Hence 2 is not a happy number. Design a class Happy to check if a given number is a happy number. Some of the members of the class are given below: Class Name : Happy Data Members n : stores the number Member functions: Happy( ) : constructor to assign 0 to n void getnum(intnn) : to assign the parameter value to the number n = nn int sum_sq_digits(int x) : returns the sum of the square of the digits of the number x. void ishappy() : checks if the given number is a happy number by calling the function sum_sq_digits(int) and displays an appropriate message. Specify the class Happy giving details of the constructor( ), void getnum( int), intsum_sq_digits(int) and void ishappy(). Also define a main function to create an object and call the methods to check for a happy number. [0] import java.io.*; public class Happy int n; Happy() n = 0; void getnum( int nn) n = nn; int sum_sq_digits(int x) int sum =0; while(x!= 0) int d = x%0; sum += d*d;
x/=0; return sum; void ishappy() int t = n; int sum =0; do sum = sum_sq_digits(t); if (sum > 9) t = sum; else break; while (true); if(sum == ) System.out.println(n +" is a happy number"); else System.out.println(n+" is not a happy number"); public static void main(string args[])throws IOException BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); Happy h= new Happy(); System.out.println("Enter a number "); int n = Integer.parseInt(br.readLine()); h.getnum(n); h.ishappy(); SECTION C Answer any two Question 0. Write a Java program to create a Text file in output mode and store 0 students 5 subject marks, with total and average. [5] import java.io.*; public class TextFile public static void main(string args[])throws IOException BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String file = "Textfile.txt";
FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); PrintWriter pw = new PrintWriter(bw); for(int i = 0; i< 2; i++) int m[] = new int[5]; System.out.println("Enter your name"); String name = br.readline(); pw.println(name); int total=0; System.out.println("Enter the 5 subject marks"); for(int j = 0; j< 5; j++) m[j] = Integer.parseInt(br.readLine()); pw.println(m[j]); total += m[j]; int avg = total / 5; pw.println(total); pw.println(avg); pw.close(); bw.close(); fw.close(); Question. Write a program to open an existing Binary file and read the String Name, int age, and String Address. Display the same on the screen as follows. [5] Name : XXXXXXXXXXXXXXXXX Age : XX Address : XXXXXXXXXXXXXXXXX import java.io.*; public class BinaryFile public static void main(string args[])throws IOException File fl=new File("sample.dat"); FileInputStream fout=new FileInputStream(fl); DataInputStream dout=new DataInputStream(fout); boolean check=false; while(!check) try String name=dout.readutf(); int age= dout.readint(); String address=dout.readline(); System.out.println("Name : " + name); System.out.println("Age : " + age); System.out.println("Address : "+ address); catch(eofexception e)
check = true; dout.close( ); fout.close( ); Question 2.. What are two types of stream classes we have in JAVA? Name them. [2] The two types of Stream classes are Byte Stream and Character Stream 2. Write the steps to input from a text file. [3]. Create a FileReader object 2. Link the above object to the BufferedReader object 3. Now read the content of the text file using readline(). 4. Read the file till the end of the file. 5. Close all the object. ********