ICSE Important Java Questions with Answers
ICSE Class – X
Computer application very
important Question and Answers
Q/N 1- Give one example each of a primitive data type and a
composite data type.
Ans. Primitive Data Types – byte, short, int, long, float, double, char, boolean Composite Data Type – Class, Array, Interface
Ans. A unary operator
requires a single operand whereas a binary operator requires two operands.
Examples of Unary Operators – Increment ( ++ ) and Decrement ( —
) Operators
Examples of Binary Operators – +, -, *, /, %
Q/N 3- Differentiate between call by value or pass by value
and call by reference or pass by reference.
Ans. In call by
value, a copy of the data item is passed to the method which is called whereas
in call by reference, a reference to the original data item is passed. No copy
is made. Primitive types are passed by value whereas reference types are passed
by reference.
Ans. Math.sqrt ( 2 *
a * s + Math.pow ( u, 2 ) )
( or )
Math.sqrt ( 2 * a * s + u * u )
(i) Division by a variable that contains a value of zero.
(ii) Multiplication operator used when the operation should be
division.
(iii) Missing semicolon.
Ans.
(i) Runtime Error
(ii) Logical Error
(iii) Syntax Error
(i) default constructor
(ii) parameterized constructor.
Ans.
public class Integer
{
int x;
public Integer() {
x = 0;
}
{
x = num;
}
}
Scanner sc = ___________ Scanner( ___________ )
Ans. Scanner sc = new
Scanner ( System.in );
Ans. An array is a
reference data used to hold a set of data of the same data type. The following
statement declares an integer array of 10 elements -
int arr[] = new int[10];
(i) Makes several passes through the array, selecting the next
smallest item in the array each time and placing it where it belongs in the
array.
(ii) At each stage, compares the sought key value with the key value of the middle element of the array.
Ans. (i) Selection
Sort
(ii) Binary Search
Ans. Variables and
Methods whwith the public access modie the class also.
Q/N 11- Q/N - What are the values of x and y when the
following statements are executed?
int a = 63, b = 36;
boolean x = (a < b ) ? true : false; int y= (a > b ) ? a :
b ;
Ans. x = false
y = 63
char c = 'A':
int n = c + 1;
Ans. The
ASCII value for ‘A’ is 65. Therefore, n will be 66.
int x=4;
x += (x++) + (++x) + x;
Ans. x = x + (x++) + (++x) + x
x = 4 + 4 + 6 + 6
x = 20
Q/N 14- Give the output of the following program segment:
double x = 2.9, y = 2.5;
System.out.println(Math.min(Math.floor(x), y));
System.out.println(Math.max(Math.ceil(x), y));
Ans.
2.0
3.0
Explanation : Math.min(Math.floor(x), y) = Math.min ( 2.0, 2.5 ) = 2.0
Math.max(Math.ceil(x), y)) = Math.max ( 3.0, 2.5 ) = 3.0
Q/N 15- State the output of the
following program segment.
String s = "Examination";
int
n = s.length();
System.out.println(s.startsWith(s.substring(5,
n)));
System.out.println(s.charAt(2)
== s.charAt(6));
Ans.
false
true
Explanation : n = 11
s.startsWith(s.substring(5,
n)) = s.startsWith ( “nation” ) = false
( s.charAt(2) ==
s.charAt(6) ) = ( ‘a’== ‘a’ ) = true
(i) Converts a string to a primitive float data type
(ii) Determines if the specified character is an uppercase character
Ans. (i)
Float.parseFloat(String)
(ii)
Character.isUpperCase(char)
String s1 = "Computer", s2 = "Applications";
a = (s1.compareTo(s2));
b = (s1.equals(s2));
Ans. Data type of a is int and b is boolean.
ASCII value of ‘C’ is
67 and ‘A’ is 65. So compare gives 67-65 = 2.
Therefore a = 2
b = false
String s = "malayalam";
System.out.println(s.indexOf('m'));
System.out.println(s.lastIndexOf('m'));
Ans.
0
8
int f = 1, i;
for (i = 1; i <= 5; i++)
{
f *= i;
System.out.println(f);
}
Ans.
int f = 1, i = 1;
while (i <=
5)
{
f *= i;
System.out.println(f);
i++;
}
Q/N 20- In the program given below,
state the name and the value of the
(i) method argument or argument
variable
(ii) class variable
(iii) local variable
(iv) instance variable
class
myClass {
int
y = 2;
myClass
obj = new myClass();
System.out.println(x);
obj.sampleMethod(5);
int
a = 6;
System.out.println(a);
}
System.out.println(n);
System.out.println(y);
}
}
Ans. (i) name = n
value =5
(ii) name = y value = 7
(iii) name = a value = 6
(iv) name = obj value = new MyClass()
Q/N 21- What is meant by
precedence of operators? Ans. Precedence of operators refers to the order in
which the operators are applied to the operands in an expression. For example,
* has higher precedence than +. So, the expression 8 + 2 * 5 will evaluate to 8
+ 10 = 18
Q/N 25- What are the types
of casting shown by the following examples? i) double x
=15.2; int y =(int) x; ii) int x =12; long y = x; Ans. i)
Explicit casting ii) Implicit
casting
Q/N 26- Name any two wrapper
classes. Ans. Byte,
Short, Integer, Long, Float, Double, Boolean, Character
Q/N 28- Write statements to
show how finding the length of a character array and char[] differs from
finding the length of a String object str. Ans. The length of a character array is found by
accessing the length attribute of the array as shown below: char[] array = new char[7]; int lengthOfCharArray = array.length; The length of a String object is found by invoking
the length() method which returns the length as an int. String str = “java”; int lengthOfString = str.length();
Q/N 29- Name the Java
keyword that: (i) indicates a method has no return type. (ii) stores the address of the currently
calling object. Ans. i) void ii) this
Q/N 30- What is an
exception? Ans. An
exception is an unforeseen situation that occurs during the execution of a
program. In simpler words, they are the errors that occur during the execution
of a program. The JRE throws an Exception object to indicate an exception which
contains the information related to that exception.
Q/N 32- State the values
stored in variables str1 and str2
Q/N 33- What does a class
encapsulate? Ans. A
class encapsulated the data (instance variables) and methods.
Q/N 34- Rewrite the
following program segment using the if..else statement. comm
=(sale>15000)?sale*5/100:0; Ans.
if ( sale > 15000 ) {
comm = sale * 5 / 100;
} else {
comm = 0;
}
Q/N 35- How many times will
the following loop execute? What value will be returned?
int x = 2, y = 50;
do{
++x;
y-=x++;
}while(x<=10);
return y;
Ans. In the first iteration, ++x will change x
from 2 to 3. y-=x++ will increase x to 4. And y becomes 50-3=47. In the
second iteration, ++x will change x from 4 to 5. y-=x++ will increase x to 6.
And y becomes 47-5=42. In the
third iteration, ++x will change x from 6 to 7. y-=x++ will increase x to 8.
And y becomes 42-7=35. In the
fourth iteration, ++x will change x from 8 to 9. y-=x++ will increase x to 10.
And y becomes 35-9=26. In the
fifth iteration, ++x will change x from 10 to 11. y-=x++ will increase x to 12.
And y becomes 26-11=15. Now the
condition x<=10 fails. So, the
loop executes five times and the value of y that will be returned is 15.
Q/N 36- What is the data
type that the following library functions return? i)
isWhitespace(char ch) ii)
Math.random() Ans. i) boolean ii) double
Q/N 37- Write a Java
expression for ut + ½ ft2 Ans. u * t + 0.5 * f * Math.pow ( t, 2)
Q/N 38- If int n[] ={1, 2,
3, 5, 7, 9, 13, 16} what are the values of x and y?
x=Math.pow(n[4],n[2]);
y=Math.sqrt(n[5]+[7]);
Ans.
n[4] is 7 and n[2] is 3. So, Math.pow(7,3) is
343.0. n[5] is 9 and n[7] is 16. So Math.sqrt(9+16) will
give 5.0. Note that pow() and
sqrt() return double values and not int values.
Q/N 39- What is the final
value of ctr after the iteration process given below, executes?
int ctr=0;
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j+=2)
++ctr;
Ans.
Outer loop runs five times. For each iteration
of the outer loop, the inner loop runs 3 times. So, the statement ++ctr
executes 5*3=15 times and so the value of ctr will be 15.
Q/N 40- Name the methods of
Scanner class that: i) is used to input an integer data from
standard input stream. ii) is used
to input a string data from standard input stream. Ans. i)
scanner.nextInt() ii)
scanner.next()
Q/N 41. What is the difference between an object and a
class? Ans. 1. A class is a blueprint or a prototype of a
real world object. It contains instance variables and methods whereas an object
is an instance of a class. 2. A class exists in the memory of a computer while
an object does not. 3. There will be only one copy of a class whereas multiple
objects can be instantiated from the same class.
Q/N 42. What does the token
‘keyword’ refer to in the context of Java? Give an example for
keyword. Ans. Keywords are reserved
words which convey special meanings to the compiler and cannot be used as
identifiers. Example of keywords : class, public, void, int
Q/N 43. State the difference
between entry controlled loop and exit controlled loop. Ans. In
an entry controlled loop, the loop condition is checked before executing the
body of the loop. While loop and for loop are the entry controlled loops in
Java. In exit controlled loops, the loop condition is checked after executing
the body of the loop. do-while loop is the exit controlled loop in Java.
Q/N 44. What are the two ways
of invoking functions? Ans. If the function
is static, it can be invoked by using the class name. If the function is
non-static, an object of that class should be created and the function should
be invoked using that object.
Q/N 45. What is difference
between / and % operator? Ans. / is the
division operator whereas % is the modulo (remainder) operator. a / b gives the
result obtained on diving a by b whereas a % b gives the remainder obtained on
diving a by b.
Q/N 46. State the total size in
bytes, of the arrays a [4] of char data type and p [4] of float data
type. Ans. char is two bytes. So a[4] will be 2*4=8 bytes. float is 4
bytes. So p[4] will be 4*4=16 bytes.
Q/N 47. (i) Name the
package that contains Scanner class. (ii) Which unit of the class gets called,
when the object of the class is created? Ans. (i)
java.util (ii) Constructor
Q/N 48. Give the output of the
following:
String n = “Computer
Knowledge”;
String m = “Computer
Applications”;
System.out.println(n.substring
(0,8). concat (m.substring(9)));
System.out.println(n.endsWith(“e”));
Ans. n.substring(0,8)
gives “Computer”. m.substring(9) gives “Applications”. These two on
concatenation gives “ComputerApplications”. n ends with “e”. So, it gives true.
The output is:
ComputerApplications
true
Q/N 49. (d) Write the
output of the following: (i) System.out.println (Character.isUpperCase(‘R’));
(ii) System.out.println(Character.toUpperCase(‘j’)); Ans. (i) true (ii) J
Q/N 50. (e) What is the
role of keyword void in declaring functions? Ans. void
indicates that the function doesn’t return any value.
Q/N 51. 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 p = 200;
while(true)
{
if (p<100)
break;
p=p-20;
}
System.out.println(p);
Ans.p
values changes as follows : 200, 180, 160, 140, 120, 100, 80. So, the loop
executes six times and value of p is 80.
Q/N 52. What will be the output
of the following code? (i)
int k = 5, j = 9;
k += k++ – ++j + k;
System.out.println("k=
" +k);
System.out.println("j=
" +j);
Ans.
k=6
j = 10
Explanation: k += k++
– ++j + k k = k + k++ – ++j + k k = 5 + 5 – 10 + 6 k = 6 j = 10 as it has been
incremented in the ++j operation.
Q/N 53. Output the given
code
double b = -15.6;
double a = Math.rint (Math.abs
(b));
System.out.println("a=
" +a);
Ans.
a = 16.0
Maths.abs(-15.6) will
give 15.6 and Math.rint(15.6) gives 16.0.
Q/N 54. Explain the
concept of constructor overloading with an example . Ans. A
class can have more than one constructor provided that the signatures differ.
This is known as constructor overloading. Example:
class Age
{
int age;
public Age()
{
age = -1;
}
public
Age(int age)
{
this.age = age;
}
}
Q/N 55. Give the prototype of a
function search which receives a sentence sentnc and a word wrd and returns 1
or 0 ? [2] Ans.
public boolean
function ( sentence sentnc, word wrd )
or
public int function ( sentence
sentnc, word wrd )
Q/N 56. Write
an expression in Java for z = (5×3 + 2y ) / ( x + y) [2] Ans.
z = ( 5 * Math.pow ( x, 3 ) + 2
* y ) / ( x + y )
Q/N 57. Write a statement each
to perform the following task on a string: (i) Find and display the position of
the last space in a string s. (ii) Convert a number stored in a string variable
x to double data type Ans. (i)
System.out.println(s.lastIndexOf(” “); (ii) Double.parseDouble(x)
Q/N 58. Name the keyword
that: (i) informs that an error has occurred in an input/output operation. (ii)
distinguishes between instance variable and class variables. Ans. (i)
throw (ii) static
Q/N 59. What are library
classes ? Give an example.
Ans. Library classes are the predefined classes which are a
part of java API. Ex: String, Scanner
Q/N 60. Write one difference
between Linear Search and Binary Search . Ans. Linear
search can be used with both sorted and unsorted arrays. Binary search can be
used only with sorted arrays.
Q/N 61- Define the term Byte
Code.
Ans. The
Java compiler compiles the source programs into an intermediate code called the
Java Byte Code which is interpreted by the Java Virtual Machine (JVM)
Q/N 62- What do you
understand by type conversion?
Ans. Type
conversion or casting is the conversion of the data type of a literal from one
type to another. There are tow types of types of casting – implicit casting and
explicit casting.
Q/N 63- Name two jump
statements and their use.
Ans. break
and continue are the two jump statements in Java. break is used to force early
termination of a loop. continue is used to move to the next iteration of the
loop while skipping the remaining code in the current iteration.
Q/N 64- What is Exception ?
Name two Exception Handling Blocks.
Ans. An
Exception is an error which occurs during the execution of a program. The
exception handling blocks are try, catch and finally.
Q/N 65- Write two advantages
of using functions in a program.
Ans. i)
Function make code reusable.
ii) Functions improve
modularity and facilitate easy debugging.
Q/N 66- State the purpose and
return data type of the following String functions:
(i) indexOf ( )
(ii) compareTo ( )
Ans. i)
indexOf() returns the index of the character or String passed as the parameter
in the string on which is invoked.
Return type is int.
ii) compareTo()
lexicographically compares the String passed as an argument to the String on
which it is invoked.
Return type is int.
Q/N 67- What is the result
stored in x, after evaluating the following expression
int x = 5;
x = x++ * 2 + 3 * –x;
Ans. x = 5
* 2 + 3 * -6
x = 10 – 18
x = -8
Q/N 68- Differentiate between
static and non-static data members.
Ans. i)
static variables belong to the class and all object share a single instance of
the static variables. Non static varaiables belong to the objects. Each object
has a copy of these members.
ii) static functions can access
only static data members. Non static function can access both static and non
static data members.
Q/N 69- Write the difference
between length and length() functions.
Ans. length is
a property of an array which gives the size of the array. length() is a
function of the String class which returns the size of the String.
Q/N 70- Differentiate between
private and protected visibility modifiers.
Ans. private
members are accessible only in the class in which they have been defined.
protected members are accessible in the class in which they have been defined
as well in the sub classes of that class.
Q/N 71- What do you
understand by data abstraction? Explain with an example.
Ans. Abstraction
refers to representing the essential features of a system without considering
all the details. Example: When we drive a car, we concentrate on how to drive
it without bothering ourselves on how the engine works and other things.
Q/N 72- What will be the
output of the following code?
int m=2;
int n=15;
for(int i = 1; i<5; i++);
m++; –-n;
System.out.println("m="
+m);
System.out.println("n="+n);
Ans.
m=3
n=14
Note that there is a
semicolon at the end of the loop. So, it is an empty loop and does’t affect the
values of m and n.
Q/N 73- What will be the
output of the following code?
char x = 'A' ; int m;
m=(x=='a') ? 'A' : ‘a’;
System.out.println("m="+m);
Ans.
m=97
Q/N 74- 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);
Ans. Following
are the iterations of the loop
3 < 6 ---- k = 1 * 3 = 3
4 < 6 ---- k = 3 * 4 = 12
5 < 6 ---- k = 12 * 5 = 60
6 < 6 ---- false
The loop will run three times
and output is 60
60
Q/N 75- Give the prototype of
a function check which receives a character ch and an integer n and returns
true or false.
Ans.
public boolean check(char ch,
int n)
Q/N 76- State two features of
a constructor.
Ans. i) A
constructor has the same name as that of a class.
ii) A constructor does not
have any return type.
iii) A constructor is
automatically called during object creation.
Q/N 77- Write a statement
each to perform the following task on a string:
Extract the second last
character of a word stored in the variable wd.
Ans.
char ch =
wd.charAt(wd.length()-2);
Q/N 78- Check if the second
character of a string str is in uppercase.
Ans.
boolean result =
Character.isUpperCase(str.charAt(1));
Q/N 79- What will the
following functions return when executed?
(i) Math.max(-17, -19)
(ii) Math.ceil(7.8)
Ans. i) -17
ii) 8.0
Q/N 80- (i) Why is an object
called an instance of a class?
(ii) What is the use of the
keyword import?
Ans. i) An object is called an instance of a class because the objects contains a copy of all the instance variables of the class.
ii) The import keyword is
used to import classes from external packages.
Comments
Post a Comment