ICSE Computer Theory QN Bank-2
ICSE Computer Theory QN Bank-2
Q. What are iteration statements? Name the iteration statements provided by Java?
Ans: Iteration statements are statements that allows a set of instructions to be executed repeatedly till
some condition is satisfied. The iteration statements provided by Java are: for loop, while loop, do-
while loop.
Q. What is the difference between entry controlled and exit controlled loop?
or
What is the difference between while and do-while loop?
Ans: while loop is known as entry controlled loop and do-while loop is known as exit-controlled
loop. The differences between these two loops are: (1) In while loop the test expression is evaluated
at the beginning where as in do-while loop test expression is evaluated at the bottom, after the body
of the loop. (2) In while loop if the test expression is false loop does not continued but in do-while
what ever the test expression the loop execute at least once.
Q. State one similarity and one difference between while and do-while loop.
Ans: Similarity: In both loops there is a chances to forget the increment statement inside the loop.
Difference: In while loop the test expression is evaluated at the beginning where as in do-while loop
test expression is evaluated at the bottom, after the body of the loop.
Q. What do you meant by an infinite loop? Give an example. [2008] ORQ. What do you meant
by an endless loop? Give an example.
Ans: Infinite loop is an endless loop whose number of iterations are not fixed.eg: for(;;)
System.out.println("java");
eg : for(i=l;i<=10;i++)
System.out.println(i);
Q 3. What does a break statement do in a loop ? 1
Ans. A break statement terminates the current loop and proceeds to the first
statement that follows the loop. Example :
forCi =l;i<10;i++)
if( i = =5 ) break;
System.out.println(i);
In the above program the loop will break at i = 5 so the output of the program will be: 1 2 3 4
Ans. The loop that executes endlessly is an infinite loop. A loop becomes infinite if
the condition or the updation statement is missing from fee loop.
Example :
(i) for(;;)
{}
(ii) for(i=l;i<=10; )
System.out,println(i);
(iii)for(i=l; ;i++)
System. out.println(i); All the above are
examples of an infinite loop.
Q 6. Convert the following segment into an equivalent do loop :
int x,c;
for(x=10,c=20;c>=l
0;c=c-2) x++;
Ans. x = 10,c = 20; /
d
o
{
x++;
c=
c-2;
}
while(c> = 10);
Q 7. Explain the function of each of the following with an
example : (i) break;
Ans. A Java keyword used to resume program execution at the
statement ii following the current statement. If followed by a label,
the program resumes at the labeled statement. Example :
for(int i=l;i<=10;i++)
Output : 12 3 4
(ii) continue;
Ans. A Java keyword used to resume program execution at the end of the cm If followed by a label,
continue resumes execution where the label occurs. Example :
for(int i=l;i<=10;i++)
{
if( i==5 | | i==7 | | i==9) continue;
System.out.print(i+ " ");
}
Output :
1 2 3 4 6 8 10
Q 8. What are entry and exit controlled loops ? Explain. Ans. Entry controlled loops are where
test-condition is evaluated before alk entry into the loop and execute the body. The entry to the loop is
allowed oi
-condition is true.For example: while and for loop are entry controlled loops-Exit controlled loop^
are those where test condition is evaluated after the exe the body of a loop. In this kind of loop the first
entry, to execute the loop is)
ut any condition but the second entry requires the test-condition to be true. While is an example of exit
controlled loop.
Write the syntax of while loop and do..while loop.
Syntax of while loop : initialization while(test condition) {
loop body
updation statement
i
Syntax of do...while loop initialization do
loop body
updation statement
}while(test condition);
Functions
void call ( ) {
x=x+10;
Output:
Value of a before calling : 20
Value of a after calling : 20 Here the value of a is copied to variable x. The value of x
changes fr the value of a remains the same as in the original, i.e. 20
Q 4 How are the following passed ?
(1) Primitive types — passed by value
(2) Reference types — passed by reference
Q 5. Define an impure function.
Ans. Impure functions also called modifier functions change the state of: Example :
class Test
{
int a=0;
void change( )
a=a +20;
}
}
Here change function changes the value of the instance variable from 0
Q 6. What is a h function prototype ? Ans. The first line of any function that
includes access specifier, modified of the function, function name and the argument list is
known as function header or function prototype.
Example :
public static void sum (int a, int b)
1
Q 7. What is function signature ?
Ans. The name of the function and the arguments in the parameter list of the function
header is known as function signature.
public static void sum (int a, int b)
void changed(int x)
System.out.println( x*2);
x = z;
}
void change( )
{
System.out. println(x*2); }
Here function change( ) is a pure function, set() is not.
Q 12. How many values a function
returns ? Ans. A function returns
only one value at a time.
Q 13. What do you mean by function overloading? Explain
with the example.
Ans. The logical method of defining various functions in a class by
the same name which are differentiated by their signature is known
as function o^ Function overloading implements polymorphism.
Example :
class Overload
{
System.out.println( a*b);
System.out.println(a*a+b*b+c*c);
}
}
Constructors
Q. What is constructor?
Ans: A constructor is a member function that automatically called, when the
object is created of that class. It has the same name as that of the class name;
it has no return type and its primary job is to initialise the object to a legal
value for the class.
Q. Why do we need a constructor as a class member?
Ans: Constructor is used create an instance of of a class, This can be also
called creating an object.
Q. Why does a constructor should be defined as public?
Ans: A constructor should be defined in public section of a class, so that its
objects can be created in any function.
Q. Explain default constructor?
Ans: The constructor that accepts no parameter is called the default
constructor. If we do not explicitly define a constructor for a class., then java
creates a default constructor for the class. The default constructor is often
sufficient for simple class but not for sophisticated classes.Example: class
ant { int i; public static void main() ant nc=new ant(); }the line new
ant() creates an object and calls the default constructor, without it we have
no method to call to build our objects. once you create a constructor with
argument the default constructor becomes hidden.
Q. Explain the Parameterised constructor?
Ans: If we want to initialise objects with our desired value, we can use
parameters with constructor and initialise the data members based on the
arguments passed to it . Constructor that can take arguments are called
Parameterised constructor.Example: public class result { int per; int
tot; public result (int percentage) { per=percentage; tot=0; } }
Q. Give a syntax/example of constructor overloading. Define a class, which
accept roll number and marks of a student. Write constructor for the class,
which accepts parameter to initialise the data member. Also take care of the
case where the student has not appeared for the test where just the roll
number is passed as argument.
Ans: class student { int roll; float marks; student(int r, float
m) // constructor with two argument. { roll=r; marks=m;
} student(int r) // constructor with one argument {
roll=r; marks=0; } student() // default
constructor { roll=0; marks=0; } }
Q. Mention some characteristics of constructors.
Ans: The special characteristics of constructors are:(i) Constructors should
be declared in the public section of the class. (ii) They are invoked
automatically when an object of the class is created. (iii) They do not have
any return type and cannot return any values. (iv) Like any other function,
they can accept arguments. (v) A class can have more than one constructor.
(vi) Default constructor do not accept parameters. (vii) If no constructor is
present in the class the compiler provides a default constructor.
Q. State the difference between Constructor and Method. [2005]
Ans: The function has a return type like int. but the constructor has no return
type. The function must be called in programs where as constructor
automatically called when the object of that class is created.
Q. Enter any two variables through constructor parameters and write a
program to swap and print the values. [2005]
class swap{ int a,b; swap(int x,int y) { a=x; b=y; } public void
main(String args[]) { int t=a; a=b; b=t; System.out.println("the value
of a and b after swaping : "+a+" "+b); }}
Q. What are the types of Constructors used in a class?
Ans: The different types of constructors are as follows:i. Default
Constructors.ii. Parameterized Constructor.iii. Copy Constructors.
Q. Define Copy constructors.
Ans: A copy constructors initializes the instance variables of an object by
copying the initial value of the instant variables from another objects.
e.g.class xyz{ int a,b; xyz(int x,int z) { a=x; b=y; } xyz(xyz p) {
a=p.x; b=p.y; }}
1. What is the use of Constructor function ?
It is a special function created with the same name as that of a class, has no return type and which
initializes object by assigning values to the data members of the class.
5. What are temporary instances of a class and how it is created ? Give one example also.
It is an anonymous instance that is created to perform one particular job within a new statement.
Temporary instances are created by calling class constructor using new keyword and
explicit method within the statement which performs the job. imple :
new obj.show();
Ans. A Constructor function that the compiler creates for programs by itself there are no user defined
constructors in a class. The default, constructor assigns to all numeric values and null to char and
String values.
Q 7. Define Instance Variable. Give an example of the same. Ans. The variable whose separate
copy is made for all the objects of a class. If there are 20 objects created of a class there will be 20
separate copies of the instance variable Example :
class Test
{
String
name; int
age:
double percent;
}
Here name, age and percent are instance variables of the class Test
Q. What are literals? How many types of integer literals are available in
Java?
Ans: A literal is sequence of characters used in a program to represent a
constant value. For example 'A' is a literal that represents the value A of type
char, and 17L is a literal that represents the number 17 as value of type long.
Different types of literals available in Java, they are: Integer literal, Float
literal, Boolean literal, Character literal, String literal and null literal.
Q. How many integer constants are allowed in Java? How are they
written?
Ans: Java allows three types of integer constants: Octal (base 8), Decimal
(base 10), and Hexadecimal (base 16). An Octal integer must be started with
a zero '0', a Hexadecimal integer starts with a '0X', all others are treated as
decimal integer constant.
Q. How many bytes occupied by the following data types: byte, short,
int, long, float, double, char, boolean.
Ans: char-2 byte, byte-1 byte, short-2 bytes, int-4 bytes, long-8 bytes, float-4
bytes, double-8 bytes, boolean-Java reserve 8 bits but only use 1 bit.
Q. What is the range of the following data types: byte, short, int, long,
float, double, char, boolean.
Ans: byte -> -128 to 127short -> -32768 to 32767int -> -231 to 231-1long
->-263 to 263-1float -> -3.4x1038 to 3.4x1038double -> -1.7x10308 to
1.7x10308char -> 0 to 65536boolean - > true or false
Q. What is the largest and smallest value for floating point primitive
data types float?
Ans: The smallest value is -3.4E+38 and largest values is 3.4E+38 of
floating point data type.
Q. What do you mean by operator and write the name of all operators
given in your textbook.
Ans: The operations are represented by operators and the object of the
operations are referred to as operands. The types of Operators available in
Java are: 1. Arithmetic 2. Increment/Decrement 3. Relational 4. Logical 5.
Shift 6. Bitwise 7. Assignment 8. Conditional 9. [] operator 10. new operator
11. (type) cast Operator 12. () operator. 13. dot operator.
Q. What is operands?
Ans: An operator acts on different data items/entities called operands.
Q. What do you mean by type casting? What is the type cast operator?
Ans: The explicit conversion of an operand to a specific type is called type
casting. The operator that converts its operand to a specified type is called
the typecast operator. The typecast operator is ( ) in Java and is used as
(type-to-be-converted-in)
Q. What is a bytecode?
Ans: Bytecode is a set of pseudo mechanic language instructions that are
intermediate code obtained on compilation of Java source code and is
understood by the JVM (Java Virtual Machine) and are independent of the
underlying hardware.
Q. What is an Object?
Ans: An Object is an identifiable entity with some characteristics and
behavior. E.g. take a class 'Car'. A car class has characteristics like colour,
gears, power, length etc. now we create the object of that class 'Car' namely
'Indica'.
Q. What is an abstraction?
Ans: An abstraction is a named collection of attributes and behaviors
required to represent an entity or concept for some particular problem
domain.
Q. What is a statement?
Ans: Statements are the instructions given to the computer to perform any
kind of action, as data movements, making decision or repeating action.
Statements form the smallest executable unit and terminated with semi-
colon.
ARRAYS
Q 1. What do you mean by an Array ?
.Ans. An array is a collection of items of same data type that are referred by one name ■but different
index numbers. The values are stored in contiguous memory locations. Each memory location store's
only one value.
Q 5. What is the pre-requisite condition for binary search ? Ans. The binary search works only if
the list is arranged in ascending or descen order.
Q 7. Define sorting.
Ans. Sorting of an array means arranging the array elements in a specified order i.i either in ascending
or descending order.
Q 8. What is Selection sort technique ? Ans. Selection sort is a sorting technique where next
smallest(or next largest) element is found in the array and moved to its correct position, e.g. the
smallest element^ should be at first position, second smallest should be at second position and so on.
Q 10. Find and correct the errors in the following program segment: int n[ ] = (2,4.6,8.101:
for (int i = System. Ans. The correct int n[ ]= -for (int System.od.:
for (int i = 0; i < = 5; i ++) System.out.println("n[" + i
+ "] = " + n[i ]); Ans. The correct form of the program is
: int n[ ]= { 2,4,6,8,10}; for (int i = 0; i < 5; i ++)
System.out.println("n[" + i + "] = " + n[i ]»:
Q 11. Define searching in an array and what are types of search techniques ? Ans. The process of
finding an element from either an ordered or unordered list of data or array is known as searching.
The following two searching methods are :
(i) Linear search
(ii) Binary search
The method of searching any desired element from an array by comparing each element in
consecutive sequence one by one is known as linear search or sequential search. The method of
searching any desired element from an ordered array in minimum number of comparisons is known as
binary search.
LIBRARY CLASSES
Q 1. What do you mean by a Library ?
Ans. A library is a collection of pre-defined functions or methods which has some meaningful
instructions to the compiler which helps in executing the instructions in a program.
System.out. println(a);
}
Exception ?
Ans. Exception refers to some contradictory or unusual situation which can be encountered while executing a program.
Q 12. What are the two types of errors ? Ans. Errors are of two
types :
(i) Compile time errors : These are errors resulting out of violation of programming
language's grammar rules.
Page 32 of 48
ICSE Computer Theory Short Question Bank
Example : misspell keywords, missing semi-colon, wrong use of brackets. AH syntax errors are reported during
compilation.
(ii) Run time errors : The errors that occur during runtime because of unexpected
situations. Such errors are handled through exception handling routines of Java.
Examples : Division by zero, array index out of bound, string index out of bound etc.
Q 14. What do the following functions return for : [4] String x = "hello"; String y =
"world";
(i) System.out.prinln(x+y);
(ii) System.out.println(x.lengthO);
) System.out.println(x.charAt(3));
(iv) System.out.println(x.equals(y));
Ans. (i) hello world
(ii) 5
(iii) 1
(iv) false
Q 15. Differentiate between toLowerCaseO and toUpperCaseO methods.
Ans. toLowerCase( ) converts all the upper case characters of the string into 1( case.
The other characters remain unaffected.
toUpperCase( ) converts all the lower case characters of the string into upper The other characters remain
unaffected. Example : String s= "Hello";
System.out.printIn(s.toUpperCase( )); will print HELLO
System.out.printIn(s.toLowerCase( )); will print hello
Q 16. What will be the output for the following program segment ?
String s = new String("abc");
System.out.println(s.toUpperCase()); Ans. ABC
Page 33 of 48
ICSE Computer Theory Short Question Bank
Q. WAP:
Method to accept a string sentence as parameter and extract the words of the sentence and count them. Find
frequency of uppercase, digits in the sentence. Find ascii value of the characters.
Q.WAP:
Method to accept an integer num as parameter and find the number of digits and the sum of the digits
Q. What is the output?
(a) System.out.print(Math.abs (Math.ceil(-4.3));
(b) System.out.println(Math.floor(144.99));
(c) System.out.println(Math.max(Math.min(15, 25), 5));
(d) System.out.println(Math.abs(Math.ceil(-4.5));
(e) System.out.println(Math.rint(3.96));
(f) Math.max (Math.min ( Math.log(Math.pow(Math.sqrt(100),(2+5*4/2))) ,
Math.pow(Math.sqrt(9),3)) ), Math.random()*10));
To print the integer part of a real number and to print the decimal part
(g)
separately.
(h) System.out.print((char) ("Application".lastlndexOf ('O')));
(i) double e = Math.pow(Math.sqrt(25), 3);
(j) System.out.print(e);
(k) Give the output of the following statements :
System.out.println( “Reena”.equals(“reena”));
(l) System.out.println("APTITUDE".compareTo("ATTITUDE")
(m) Find substring of APTITUDE from 4th character to 6th character
(n) Find 2nd character from end of string APTITUDE
(o) Replace all ‘T’ with ‘C’ in APTITUDE
Q.Give the output and the datatype of the following statements :
System.out.println(String(123));
a) System.out.println(Integer.valueOf(“27541415”));
b) Analyse the following program segment and determine how many
times the loop will be executed and what will be the output of the program segment ?
int k=1,i=2;
while (++i < 6) k*=i;
System.out.println(k);
(c) Rewrite if-statement as ternary operator statement
int a = 5;
String w= “”;
if(a % 2 == 0)
w=“Even”;
else
w=“Not Even”;
Ans: String w=(a % 2 == 0)? “Even”: “Not Even”;
Page 34 of 48
ICSE Computer Theory Short Question Bank
(i) Name the method that converts a double type value into characters
(ii) Name a class that converts bytes to characters
(iii) Name the package that contains the wrapper classes.
(iv) Name the package that contains the System class.
(v) Name the OOP feature that illustrates the following concept :- There can be two
methods having the same name in Java.
(vi) The conversion of a bigger data type to a smaller data type.
(vii) The operator used to create new object of a class.
iv) The variables whose single copy is made for all the objects of a class.
v) The environment that allows developers to compile, debug and run the program
vi) Object communicate with one another by the use of ?
vii) A Construct that binds one or more primitive data types together to be used a single
Data Type.
viii) indicates that a method has no return type
ix) stores the address of the currently-calling object.
x) the smallest integer data type.
xi) Name the keyword that a class uses to inherit another class
xii) Name the part of the class that allocates memory to an object
xiii) Name the package that contains Scanner class.
(b) Show the working of Bubble Sort of the array elements {5, 1, 12, -5, 16} for each pass
(c) Show the working of Selection Sort of the array elements {5, 1, 12, -5, 16}
Page 35 of 48
ICSE Computer Theory Short Question Bank
Page 36 of 48
ICSE Computer Theory Short Question Bank
Member methods:
(i) void input() To input and store the accession number, title and author.
(ii) void compute() To accept the number of days late, calculate and display the fine
charged at the rate of Rs.2 per day.
(iii) void display() To display the details in the following format:
Accession Number Title Author
Write a main method to create an object of the class and call the above member methods.
Q.3 Given below is a hypothetical table showing rates of Income Tax for male citizens
below the age of 65 years:
Taxable Income (TI) in Rs. Income Tax in Rs.
Does not exceed Rs. 1,60,000 Nil
Is greater than Rs. 1,60,000 & less than (TI – 1,60,000) x 10%
or equal to Rs. 5,00,000
Is greater than Rs. 5,00,000 & less than [(TI – 5,00,000) x 20%] + 34,000
or equal to Rs. 8,00,000
Is greater than Rs. 8,00,000 [(TI – 8,00,000) x 30%] + 94,000
Write a program to input the age,gender (male or female) and Taxable income of a
person.
If the age is more than 65 years or the gender is female, display “wrong category”.
If the age is less than or equal to 65 years and the gender is male, compute and
display the Income Tax payable as per the table given above.
Q.5 Write a program to accept a string. Convert the string to uppercase. Count and output
the number of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Sample Output: 4
Page 37 of 48
ICSE Computer Theory Short Question Bank
Q.6 Write a program in Java to input the zone code and salesof 10 salesmen. The zone
code varies from 1 to 4. Find the total sales made in each zone and ouput in the
following format:
ZONE CODE TOTAL SALES IN THE ZONE
1
2
3
4
And also output the following format:
ZONE WITH ZONE CODE SALES
Maximum Sales
Minimum Sales
Mean Sales ---
Q.7 Write a program that reads the following list of cities into an one dimensional array.
The program should accept the name of a city in the list as input and display its position
in the list as the output. The program should be designed to give an error message when a
city is asked whose name is not given in the list. To stop the user input “XXX” is to be
entered as input.. Sort the list of cities in alphabetical/ascending order using the Selection
Sort technique. Sort the list of cities in descending order using the Bubble Sort technique.
Q.8 WAP for Binary Search of an array. Find the location and values of the largest and
smallest elements.
1. import java.io.*;
class Library {
int acc-num;
String title;
String author;
Page 38 of 48
ICSE Computer Theory Short Question Bank
acc_num = num;
title = tit;
author = auth;
Days=d;
Fine = days*2;
System.out.println(“Fine is = “+fine);
String a = br,readLine();
int d = Integer.parseInt(a);
lib.compute(d);
Page 39 of 48
ICSE Computer Theory Short Question Bank
2. import java.io.*;
class SeriesOverload {
System.out.println(“Value of n = “);
double sum = 0;
sum += 1.0/I;
return sum;
System.out.println(“Value of a = ” + a);
System.out.println(“Value of n =” + n);
double sum=0;
int i,j;
return sum;
Page 40 of 48
ICSE Computer Theory Short Question Bank
2. import java.io.*;
class IncomeTax{
int age;
int gender;
double income;
double tax;
double p, d, net;
System.out.println(“*******************INCOME
TAX*****************”);
age = Integer.parseInt(br,readLine());
Page 41 of 48
ICSE Computer Theory Short Question Bank
income = Double.parseDouble(br,readLine());
System.out.println();
char choice;
choice = (char)System.in.read();
switch(choice) {
else if(age <= 65 and income > 160000 && income <= 500000) {
tax=(income-16000)*0.1;
else if(age <=65 && income > 500000 && income <= 800000) {
tax=(income-500000)*0,2+34000;
System.out.println(“________________________”);
System.out.println(“________________________”);
Page 42 of 48
ICSE Computer Theory Short Question Bank
tax=(income-800000)*0.3+64000;
break;
System.out.println(“________________________”);
System.out.println(“Wrong Category”);
System.out.println(“________________________”);
break;
2. import java.io.*;
class SwitchCase {
int n=0;d,ctr,choice;
int t;
Page 43 of 48
ICSE Computer Theory Short Question Bank
try {
choice = Integer.parseInt(br.readLine());
catch(Exception e) {
System.out.println(“Input Error!”);
System.exit(0);
try {
n = Integer.parseInt(br.readLine());
catch(Exception e) {
System.out.println(“Input Error!”);
System.exit(0);
Page 44 of 48
ICSE Computer Theory Short Question Bank
d=1;
ctr = 0;
switch(choice) {
case 1:
while(d<=n) {
if(n= =1)
System.exit(0);
if(n%d = = 0)
ctr++;
d = d + 1;
if(ctr= =2)
int x,y;
x=0;
y=0;
int count=0;
String a;
Page 45 of 48
ICSE Computer Theory Short Question Bank
char ch1,ch2;
System.out.println(“Enter String::”);
a = br.readLine();
x = a.length();
if(a == null)
return;
for(y=0;y<x-1;y++) {
ch1 = strU.charAt(y);
ch2 = strU.charAt(y+1);
if((int)ch1 == (int)ch2)
6) class Sales {
Page 46 of 48
ICSE Computer Theory Short Question Bank
cont=true;
while(cont)
zc = Integer.parseInt(br.readLine());
ans = (char)br.read();
cont=false;
for(int i=0;i<4;i++)
Page 47 of 48
ICSE Computer Theory Short Question Bank
if(max==zonesale[i])
maxzone = i;
min = zonesale[i];
minzone = i;
Page 48 of 48