CFQ ISC Computer Science XII (1)
CFQ ISC Computer Science XII (1)
PREFACE
Table of Contents
ISC-CLASS XII
Computer Science
1. [Scope]
For the given code snippet:
int i;
for( i = 5; i>=1; i--)
System.out.println( );
System.out.println(i);
(a) Zero will be displayed as variable i has local scope with respect to the loop.
(b) Zero will be displayed as variable i has global scope with respect to the loop.
(c) Error – Undeclared variable i.
(d) Output will be 1. (Analysis)
2. [StringBuffer]
m and n are two StringBuffer objects. The method call, m.append(n)will:
S.No. Questions
3. [StringBuffer]
The code snippet given below will result in:
4. [Recursion]
Recursion takes toll on memory. Which of the following option is the reason for it?
(a) Every recursive call is placed in a call stack memory.
(b) Every recursive call is resolved in Last-In-First-Out (LIFO) manner.
(c) Values of local variables are maintained until each of the calls are resolved.
(d) All of the above. (Understanding)
5. [Recursion]
If binary search technique uses recursion instead of iteration, the code:
i. will contain two base cases.
ii. will contain two recursive cases.
iii. when executed will lead to an exception, stack overflow.
Which of the above is valid?
(a) i and ii
(b) ii and iii
(c) i and iii
(d) Only iii (Evaluate)
6. [Arrays]
Assume int [ ] [ ] x = { {8,9} , {5,6,7},{1,2,3,4}}, what are x[0].length, x[1].length, and
x[2].length ?
(a) 2, 3 and 3
(b) 2, 3 and 4
(c) 3, 3 and 3
(d) 2, 2 and 2 (Application)
S.No. Questions
7. [Arrays, Strings]
Passwords are stored as two Strings-one is the user entered password saved in
StringA, the other is a password stored by the system in StringB.
If StringA must be checked with StringB, for validity, which one of the following
String functions are needed to be used?
(a) charAt( )
(b) equals( )
(c) equalsIgnoreCase( )
(d) startsWith( ) (Understanding)
8. [Arrays]
What is the outcome of the following code snippet:
public class Test
{
public static void main( )
{
char [ ][ ] matrix = {{'a','b','c'},{'p','q','r'},{'x','y','z'}};
for( int i=0 ; i<3 ; i++)
System.out.print( matrix[i] [1] + " " );
}
}
(a) a b c
(b) a p x
(c) p q r
(d) b q y (Analysis)
S.No. Questions
9. [Strings]
For the following code snippet choose the correct output:
int x =234;
String str= String.valueOf(x) ;
System.out.println(str.length( ));
System.out.println( x + x );
System.out.println( str + str );
a. b. c. d.
(Analysis)
S.No. Questions
S.No. Questions
16. [Inheritance]
In single inheritance, which one of the following keywords should NOT be used.
(a) extends
(b) this
(c) super
(d) implements (Application)
17. [Inheritance]
interface A interface B
{ {
final static int x=5; final static int y=5;
abstract void abc( ); abstract void abc( );
} }
Assume that class Check implements interfaces A and B,
how many times the method abc( ) will be defined in class
Check ?
(a) Twice
(b) Once
(c) Compile time error
(d) Runtime error (Application)
S.No. Questions
The Central Processing Unit (CPU) scheduling follows the principle of _ data
structure.
For the above Binary Tree, which traversal order will arrange the elements in ascending
order?
(a) Post order
(b) Pre order
(c) In order
(d) Postfix order (Application)
The questions in this section have two statements marked Assertion and Reason. Read both
S.No.
the Questions
statements carefully and choose the correct option.
21. [Recursion]
Assertion (A): Recursion utilises more memory as compared to iteration.
Reason (R): Absence of base case leads to infinite recursion.
(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 true, but R is false.
(d) A is false, but R is true. (Analysis)
Assertion (A): In Boolean Algebra, dual of the Boolean expression (A+B)’.1 is equal
to 0.
Reason (R): In Boolean Algebra, the complement of an OR operation is equal to the
AND operation of complement of the individual variables.
Assertion (A): The truth table for the XOR gate shows that the output is HIGH only
when an odd number of inputs are HIGH.
Reason (R): The XOR gate performs addition modulo 2, thus producing a HIGH
output when the number of HIGH inputs is odd.
S.No. Questions
S.No. Questions
26. [Inheritance]
Study the given two classes below and identify the error if any, in the given code.
(Evaluate)
27. [Inheritance]
What will happen if data members of super class are made private instead of protected?
(Evaluate)
28. [Inheritance]
What will happen if void show( ) in subclass Car is given access specifier as private?
(Evaluate)
29. [Interface]
All methods declared in the interface are abstract. Where will the body of these abstract
methods be defined?
(Application)
S.No. Questions
30. [Inheritance]
Based on the above given image, write the java statement to create the class Child.
(Create)
31. [Polymorphism]
In the given code snippet, which feature of object-oriented programming concept is
being exhibited by the substring( ) method?
String s1="sandwich";
System.out.println(s1.substring(4));
System.out.println(s1.substring(1,4));
(Analysis)
S.No. Questions
(Understanding)
TeacherA TeacherB
(Application)
S.No. Questions
double m = 1, n = 1;
for (int i = 0; i< x; i++)
{
m *= Math.random( );
}
for (int j = 0; j < y; j++)
{
n *= Math.random( );
}
(Analysis & Evaluate)
The student modified the code later to reduce the number of lines in the code.
void displayMessage( )
{
int i =1;
while(i<=4)
{
System.out.println("Hello World");
i++;
}
}
Compare the above code snippets with respect to the time complexity.
(Understanding & Analysis)
S.No. Questions
44. [String]
The java statement to print the position of 2nd occurrence of the letter 'I' within the
word "MICHIGAN".
(Recall)
45. [String]
Find the output:
System.out.println("RocKet".substring(0, 4).compareTo( "Rock".replace('m' , 'o')));
(Evaluate)
46. [Arrays]
What is the length of the array int arr[ ], if its first index is x and the last index is y ?
(Application)
47. [String]
String s1= "Gujarat";
String s2= new String("Gujarat");
String s3= new String ("Gujarat");
String s4= "Gujarat" ;
How many String objects are created in the above snippet? Justify your answer.
(Understanding)
48. [String]
Find the output of following snippet:
String S1= "AB" , S2= "BC";
for(int i=0, j=0 ; i<S1.length( ) ; i++, j++)
System.out.println(S1.charAt(i)+S2.charAt(j));
(Evaluate)
S.No. Questions
49. [Packages]
Why is the lang package termed as the default package in Java?
(Analysis & Recall)
50. [Packages]
State the purpose of the following keywords in Java:
(a) package
(b) import (Application)
51. [Packages]
Given the following code snippet:
(Evaluate)
S.No. Questions
54. [Computer Hardware]
Following is the circuit diagram of a full adder. Redraw the circuit diagram by adding
missing logic gates.
(Recall)
56. [Recursion]
Following method is a part of a class MyArray.
int check(int m[ ], int i)
{
if (i<= 0)
{
return 0;
}
return check(m, i-2 ) + m[i-1];
}
public static void main( )
{
int m[ ] = {1, 2, 3, 4};
int ans = check(m, m.length);
System.out.println(ans);
}
(a) Predict the output of the code considering there is no compilation error. Show
the working.
(b) Considering the if condition is changed to if(i<0), how will it affect the output?
(Recall & Analysis)
S.No. Questions
57. [Recursion]
A student has written the following code. It is written to check whether a string is
palindrome or not. However, the code is not giving the desired result when the parameter
“MADAM” is passed to the method isPalindrome(). Analyse the code and find out the
logical error.
58. [Recursion]
Following code gives a StackOverException.
Stack is a data structure whereas recursion is related to a method. Why does the above
code give an exception with stack overflow?
(Understanding & Application)
S.No. Questions
59. [Operation on File]
What is the difference between the following two statements?
1. FileWriterfw=new FileWriter(<file name>);
2. FileWriterfw=new FileWriter(<file name>, true);
(Create)
Anurag 90 95
Brijesh 95 95
Toshali 97 100
Now, there are four places in the code marked as ?1? , ?2? , ?3? and ?4? and they must
be replaced by the statements so that the program runs correctly.
import java.util.*;
import java .io.*;
class StuFile
{
public static void calculate( )throws IOException
{
FileReader fr= new FileReader(“Student.txt”);
BufferedReader br=new BufferedReader(fr);
String t , name, name1;
int ph, co;
double avg ;
double max=0;
while( ?1? )
{
StringTokenizer st=new StringTokenizer( t );
name= ?2? ; // Student name
ph= Integer.parseInt(st.nextToken( )); // Physics marks
co = Integer.parseInt(st.nextToken( )); // Computer Science marks
avg= (ph+co)/?3? ;
System.out.println(name+"- "+ph+", "+co+", "+avg);
if ( max<avg )
{
max=avg ;
name1=name ;
} // end of if
} // end of outer while
System.out.println( “Name of students with highest average =”+name1);
?4?
} // end of calculate method
} // end of class StuFile (Create)
S.No. Questions
The class POS is created to represent the POS for cart management. Observe the given code
and answer the following questions.
import java.util.*;
public class POS
{
Cart first;
static int count=0;
static Scanner sc=new Scanner(System.in);
void viewQueue( )
{
Cart temp=first;
boolean flag=false;
System.out.println("The carts at the counter are");
while(temp!=null)
{
System.out.println(temp.cartNo);
?1?
?2?
}
if(!flag)
System.out.println("The checkout counter is empty");
else
System.out.println("number of carts currently="+count);
}
void addCart()
{
System.out.print("Enter the new cart number: ");
S.No. Questions
int no=sc.nextInt( );
Cart newCart = new Cart(no);
if(first==null)
first=newCart;
else
{
Cart temp=first;
while(temp.next!=null)
temp=temp.next;
temp.next=newCart;
}
count++;
}
void checkOutCart()
{
?1?
System.out.println("Cart being removed ="+temp.cartNo);
?2?
temp.next =null;
temp=null;
?3?
System.out.println("Carts remaining "+count);
}
}
(a) Write the code for the blanks given in the method void viewQueue( ).
(b) Mention the code for the blanks given in the method void checkOutCart( ).
S.No. Questions
(b) Observe the given tree and answer the following questions.
(i) Why can the following tree not be termed as a full binary tree?
S.No. Questions
(Application)
S.No. Questions
S.No. Questions
Column A Column B
(b) Differentiate between the keywords this and super (... ) with respect to constructor.
(c) Base class Student and derived class ICSEStudent are illustrated as given below:
S.No. Questions
A student from the Electronics department is asked to design a digital alarm for a smart
home security system for the outside area. It comprises a sensor and a digital identification
(ID) card with six-digit personal identification number (PIN). The alarm is supposed to
buzz if wrongful entry is attempted, either by breaking the glass window or by entering the
wrong PIN.
The sensor detects the door opening without scanning a digital ID card.
or
The sensor detects the door opening by breaking of the door lock.
or
The sensor detects the wrong pin entry thrice of the digital ID card while opening
the door.
Inputs
D Door opening
(In all the above cases 1 indicates Yes and 0 indicates No).
Output: A – Denotes the buzz of security alarm system (1 indicates yes and 0 indicates
no)
Draw the truth table for the inputs and outputs given above. Write the canonical POS
expression for the A (D, B, S, P).
(Understanding, Recall, Apply, Create)
S.No. Questions
(a)Reduce the above expression by using a 4-variable Karnaugh map, showing the various
groups (i.e. octal, quads and pairs).
(b) Draw the logic gate diagram for the reduced expression using NAND gate only.
Assume that the variables and their complements are available as inputs.
S.No. Questions
71. [Programme based on Numbers]
An Automorphic number is a number whose square ends with the given
number itself. E.g. (5)2 = 25, (25)2 = 625, (76)2 = 5776.
Design a class Automorphic to check if numbers in the given range are
Automorphic numbers or not. The member functions and data members of the
class are given below:
Sample output
List of Automorphic numbers from 1 to 1000 :
Number Square
1 1
5 25
6 36
25 625
76 5776
376 141376
625 390625
Frequency of Automorphic numbers between 1 to 1000 : 7
(Create)
S.No. Questions
72. [Programme based on Strings]
Two words are called anagram of each other if both the words contain the same
letters but they are arranged in different order. Following are the three pairs of
the words which are anagrams of each other.
cat, act heart, earth silent, listen
Design the class Anagram to input two words in lower case and check if they
are anagram of each other. The member functions and data members of the
class are given below:
Class Name :Anagram
Data members/instance variables:
s1, s2 : stores the two words in lower case
l1,l2 : number of character in s1 and s2 respectively
Member functions :
Anagram(String s1, String s2): parameterised constructor to assign the accepted
values of s1 and s2 to data member
void sort(char c[]) : to sort the characters in array c[ ] in ascending
order using any sorting technique
boolean areAnagram(char[] str1, char[] str2)
: to check if str1 and str2 are anagrams of
each other and to return true or false
accordingly
void check() : to convert the two strings into two character
arrays and to display the appropriate message as
shown in the sample output by calling the
method areAnagram(char[] str1, char[] str2)
Specify the class Anagram giving details of the parameterised constructor, void
sort(char c[ ]), boolean areAnagram(char[] str1, char[] str2) and void check(
). Create one object in the main( )method and call all the methods
appropriately.
Sample input
Enter first word->earth
Enter second word->heart
Output
earth and heart are anagram of each other
(Create)
S.No. Questions
73. [Recursion program]
Design a class Spy to check if a number is a Spy number or not. A spy number
is a number where the sum of its digits equals the product of its digits. For
example, 1124 is a spy number, the sum of its digits is 1+1+2+4=8 and the
product of its digits is 1*1*2*4=8.
Class name : Spy
Data Members:
num : to store the number
Member methods:
Specify the class Spy giving details of the functions int sumOfDigits(int ), void
check( ), int prodOfDigits(int). Assume that the data member and constructor
are already defined. Define a main method to create an object and call the
functions accordingly to enable the task.
(Analysis, Application, Evaluate & Create)
S.No. Questions
74. [Object as an Argument]
A sports academy wants to conduct a friendly match between two teams with
maximum fifteen players. A class Sports contains three data members, to store
the name of the team with ‘n’ number of players and a one-dimensional array to
store height of each player. Design a class Sports to compare average height of
both the teams. The details of the members of the class are given below:
Member methods:
String compare(Sports s1, Sports s2) :to compare average height of both
the teams and return the name of
the team with the higher average
height
void display(String msg) :to display the name of the team with the
higher average height stored in msg. If
both the teams have the same average
height, then it msg should be “Both teams
have the same average height.”
Specify the class Sports giving the details of constructors, void fillHeight( ),
double averageHeight( ), String compare(Sports, Sports),void display(String).
Write the main( ) method to call methods defined in an appropriate manner.
S.No. Questions
75. [Arrays]
Given below is an example of a double dimensional array with unequal number
of rows and columns. It must be arranged in such a way that the elements in the
even rows are in ascending order and the elements in the odd rows are in
descending order , using Insertion sort technique.
Sample Input :
Row=3
Column=4
Enter the array elements
1234
9876
4567
Before sort:
1 2 3 4
9 8 7 6
4 5 6 7
After sort:
4 3 2 1
6 7 8 9
7 6 5 4
The members of the class are given below:
Class name : Mix_sort
Data members / instance variables:
a[][] : to store integers in the double dimensional array.
m : to store the number of rows .
n : to store the number of columns .
Methods / Member functions :
Mix_sort(int row, int col) : parameterised constructor to initialise the
m=row, n=col and allocate memory for a[][]
void input() : to enter the integer elements in a [][]
void ascEven(int x[]) : arrange the elements of x[] in ascending
order using Insertion sort technique.
void dscOdd(int x[]) : arrange the elements of x[] in descending order
using Insertion sort technique.
void arrange() : to arrange the rows in even position in Ascending
order using ascEven(.. ) method and rows in odd
position in descending order using dscOdd(..)
method. Finally store the sorted elements into a[][]
void display( ) : display the original a[][] and sorted a[][] .
Define the class Mix_sort with details of the constructor, void input(), void
arrange(), void ascEv(int []), void dscOdd(int []) and void display( ). Define a
main() function to create an object and call the functions accordingly to enable
the task.
(Understanding, Recall & Create)
ANSWER
1. (b) Zero will be displayed as variable i has global scope with respect to the loop.
5. (a) i and ii
6. (b) 2, 3 and 4
7. (b) equals( )
8. (d) b q y
9. (d)
21. (b) Both A and R are true, and R is not the correct explanation of A.
24. (a) Both A and R are true, and R is the correct explanation for A.
26. Call of super (...) in the body of the parameterised constructor of class Car should be the
first statement in the constructor body.
27. Though the data members with access specifier private will be inherited to subclass Car,
they will not be directly accessible in subclass Car.
28. In that case, Method overriding of void show() will fail and it will give syntax error.
29. The body for the abstract methods of interface, is defined in the class which implements
this interface.
31. The method substring( )of class String is an overloaded method, thus it is exhibiting
polymorphism.
String str = “15.45”;
32.
Double d =new Double (Double.parseDouble(str));
34. Range of byte is -128 to 127,which represents 256 different numbers including 0.
36. (a) Population- long data type, (b) Interest rate - float data type
38. Wrapper classes are part of java.lang package. Since the java.lang package is an in-built
package, an import statement is not needed. Hence, in programs where wrapper class
methods are used, programmers need not import any package.
40. O ( x + y )
41. In each iteration of the code, the size of both the numbers is reduced by a factor of at
least
2. This makes the number of iterations proportional to log N.
42. The complexity of both the codes will remain same as O(1) as the print statement is
independent of any input size.
43. false
System.out.print(″MICHIGAN″.lastIndexOf("I"));
44.
OR
System.out.print("MICHIGAN".indexOf('I',2));
45. -32
46. y-x+1
47. Three objects. This is because s2 and s3 are two objects created in the heap memory.
Value of s1 is stored in string pool and s4 refers to object it, as the values of s1 and s4
are same and it is already present in the string pool.
49. To use any class from any package one needs to import that package in the current class
by using the import statement. lang is an exception to this rule and it can be used in any
class without using import statement. Hence, it is called the default package.
50. (a) package statement enables the creation of a user defined package.
(b) import statement enables the usage of a package(built in/ user defined) in a
particular class.
51. (a) Hello Abhay, welcome to the world of programming.
(b) import java.util.*; enables us to import the built-in package util whereas import
mine.*; enables us to import the user defined package mine.
D1= A1’.A0 D3= A1.A0
52.
54.
55.
58. Due to recursion, when a method calls itself, it creates a new copy of itself on the call
stack memory. The stack stores each method call in a last-in, first-out (LIFO) order. This
means that the last method call will be the first one to get resolved.
Each time a recursive method is called, a new local variable and parameters of the
respective are pushed onto the call stack memory. In the given example the base case is
missing, hence the recursion happens infinitely. But as the call stack memory is finite,
gets full and thus at runtime, stack overflow exception is thrown
59. In the first statement, the FileWriter object fw creates an output stream ( text file )
using default mode i.e. overwrite mode.
In the second statement, the FileWriter object fw creates an output stream ( text file ) , in
append mode.
60. nextToken( ) : It returns the next token present in the String from the current tokens.
It returns the string value
.hasMoreTokens() : It checks whether there are more tokens available or not . It
returns the boolean value.
61.
?1? → (t=br.readLine()) != null
?2? → st.nextToken();
?3? → 2.0
? 4? → fr.close( );
br.close( );
2. first=temp.next;
3. count--;
63. (a) Complete binary tree. In the above binary tree all the levels of the tree are filled
completely except the lowest level nodes, which are filled from left.
(b)(i) In a full binary tree, every node possesses either 0 or 2 successors. Here, node C has
only one successor i.e. E, hence it is not a full binary tree.
(ii) 6
(iii) B and C
(c)The absolute difference of heights of left and right sub trees at any node
is less than -1 or 0.
OR
For each node, its left sub tree is a balanced binary tree.
OR
For each node, its right sub tree is a balanced binary tree.
(b) Using ‘this’ from within one constructor invokes another constructor of the same
class. Using ‘super’ keyword, the super class constructor is invoked from the sub-class
constructor.
(c)
67.
D B S P Output
0 0 0 0 0
0 0 0 1 0
0 0 1 0 0
0 0 1 1 0
0 1 0 0 1
0 1 0 1 1
0 1 1 0 1
0 1 1 1 1
1 0 0 0 1
1 0 0 1 1
1 0 1 0 1
1 0 1 1 1
1 1 0 0 1
1 1 0 1 1
1 1 1 0 1
1 1 1 1 1
Canonical POS expression:
A(D,B,S,P) = π(D+B+S+P).(D+B+S+P’).(D+B+S’+P).(D+B+S’+P’)
68.
(a)
A C G V F
0 0 0 0 0
0 0 0 1 0
0 0 1 0 0
0 0 1 1 1
0 1 0 0 0
0 1 0 1 0
0 1 1 0 0
0 1 1 1 1
1 0 0 0 1
1 0 0 1 1
1 0 1 0 1
1 0 1 1 1
1 1 0 0 0
1 1 0 1 1
1 1 1 0 0
1 1 1 1 1