Concept of Tokens and Data Types : ICSE Class 10 Board Exam Questions and Answers

1 Mark Questions

Question 1 : What is the use of the keyword import?

Answer : The ‘import’ keyword is used to include the source of other Java classes and packages in the current class so that we can use those classes. e.g., to use an ArrayList in our class you must have the below import statement in your class:

import java.util.ArrayList;

2 Marks Questions

Question 2 : Which of the following are valid comments?

i) /* Comment */

ii) /* Comment

iii) // Comment

iv) */ Comment */

Answer : The valid comments in Java are :

i) /* Comment */

iii) // Comment

Question 3 : Name the primitive data type in Java that is

i) a 64-bit integer and is used when you need a range of values under than whose provided by int.

ii) a single 16-bit unicode character whose default value is ‘/u0000’.

Answer : i) long, because it contains eight byte (64-bit) and it belongs to the int data type.

ii) char, because firstly it assumes a size of 2 byte (16-bits) and another is that unicode characters are of char data type.

Question 4 : Operators with higher precedence are evaluated before operators with relatively lower precedence. Arrange the operators given below in order of higher precedence to lower precedence.

i) &&

ii) %

iii) >=

iv) ++

Answer : The operators are arranged in order of higher to lower precedence:

i)++

ii)%

iii)>=

iv)&&

Question 5 : State one difference between the floating point literals float and double.

Answer : Difference between floating point literals float and double:

Float Literals Double Literals
The float literal has a limited range, to store decimal data item.It can hold 32-bit data. The double literal has a wider range as compared to float, to store decimal data items. It can hold 64-bit data.

Question 6 : List the variables from those given below that are composite data types:

i) static int x;

ii) arr [i]=10;

iii) obj.display();

iv) boolean b;

v) private char chr;

vi) String str;

Answer : Composite data types are  as:

i) arr[i]=10;

ii) obj.display();

vi) String str;

Question 7 : What is meant by precedence of operators?

Answer : Precedence of operator determine the grouping of terms in an expression. This affect how an expression is evaluated. Certain operators have higher precedence than other. e.g., multiplication operator has higher precedence than the addition operator.

Question 8 : What is literal?

Answer : Literal is a value that can be written directly into a Java program. A constant value in a program is denoted by a literal. It represents integer, floating point, character, boolean and string values.

Question 9 : Name the Java keyword that

i) indicates a method has no return type.

ii) stores the address of the currently calling object.

Answer : i) The void keyword, it allows us to create functions that either do not require any parameters or do not return a value.

ii) In Java, this keyword is a reference variable that refers to the current object.

Question 10 : Write a Java expression for

ut + 12 f2

Answer : Expression for

ut + 12 f2

= u * t + (1/2) * f * t * t

Question 11 : 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;

Answer : i) Explicit type casting, because here we want to assign x value into y, but x is double type variable and y is int type, so int is unable to hold the variable of double and when required downcasting.

ii) Implicit type casting, because we require upcasting here as int type is to be convert into long type.

Question 12 : Give one example each of primitive data type and composite data type.

Answer :  Primitive data type : char

Composite data type : class

Question 13 : Give one point of difference between unary and binary operators.

Answer : Difference between unary operator and binary operator:

Unary Operator Binary Operator
Unary operators work on a single operand. Binary operators work on two operands.

Question 14 : 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;

Answer : The value of x=true, and

The value of y=36

Question 15 : State the value of n and ch.

char c='A';

int n=c+1;

char ch=(char) n;

Answer : The value of n=66 and

The value of ch=B

Given, c=’A’

Therefore, ‘A’  = 65

n=c+1=65+1=66

Question 16 : What will be the result stored in x after evaluating the following expression?

int x=4;

x+ = (x++)+(++x)+x;

Answer : int x=4;

x+ = (x++) + (++x) + x ;

= x + ((x++) + (++x) + x);

= 6+ (5+5+4) = 20

Question 17 : 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

{

    static int x=7;

    int y=2;

    public static void main(String args[])

   {

        myClass obj=new myClass();

        System.out.println(x);

        obj.sampleMethod(5);

        int a=6;

        System.out.println(a);

  }

  void sampleMethod(int x)

  {

      System.out.println(n);

      System.out.println(y);

  }

}

Answer : i) method argument or argument variable = x

ii) class variable = x

iii) local variable = a

iv) instance variable = y

Question 18 : What does the token ‘keyword’ refer to, in the context of Java? Given an example for keyword.

Answer : Keywords are special tokens which have reserved use in the language. It can not be used as identifier in Java and you can not declare a field whose name is keyword, for instance.

e.g. if, new, case, goto, int, etc.

Question 19 : What is the difference between / and % operator?

Answer : Difference between / and % operator.

/ Operator % Operator
This is a division operator. This is a modulus operator.
/ can operate on integer as well as real number. % can operate on integers only.

Question 20 : What will be the output of the following code?

int k = 5, j = 9;

k+=k++ - ++j+k;

System.out.println("k ="+k);

System.out.printlin("j ="+j);

Answer : k+ = (k++) – (++j) + k;

=  k + (k++ – ++ j + k);

= 6+ (5-10+5);

k = 6

Hence, k=6, j=10

Question 21 : Differentiate between static and non-static data members.

Answer : Static data number has only one copy of instance variable that share among all the objects of the class.

Non-static data member’s has its own copy of instance variable.

Question 22 : What is the result stored in x, after evaluating the following expression

int x = 5;

x = x++*2+3*--x;

Answer : The value of x is

x = x ++ * 2 + 3 * — x;

We know the value of x = 5,

So, x = 5++ * 2 + 3* –5

= 10 + 15

= 25

Hence, x = 25

Question 23 : What do you mean by type conversion? How is implicit conversion different from explicit conversion?

Answer : Type conversion is changing a value from one data type to another type. Data type conversions are either implicit of explicit, it depends on the data capacities of the data type involved.

Difference between implicit and explicit conversion:

Implicit Conversion Explicit Conversion
It is automatically type conversion. It is completely defined within a program.
This method is used when upcasting is required like: int to long. This method is used when down casting is required like : double to byte.

Question 24 : State the difference between boolean literal and character literal.

Answer : Difference between boolean and character literal:

Boolean Literal Character Literal
Boolean literal consists of the keyword true and false. Character literal is expressed by a single character.
Boolean literal are never enclosed within quotes. A character literal constant enclosed within single quotes.

Question 25 : What is the use and syntax of a ternary operator?

Answer : Ternary operator deals with three operands. It is also called conditional assignment statement because the value assigned to a variable depends upon the logical expression. The common use of ternary operator is in checking the condition.

Syntax

Variable = (test expression)?

Value 1: Value 2

Question 26 : Write one word answer for the following.

The default initial value of a boolean variable data type.

Answer : The default initial value of a boolean variable data type is false.

Question 27 : What is the use of the keyword this?

Answer : ‘this’ keyword can be used:

i) To get reference of an object through which that method is called within (instance method).

ii) To invoke constructor of some class.

Question 28: State the difference between token and identifier.

Answer : Token –  It is the smallest element in a program that is meaningful to the compiler. These define the structure of the language. The Java token set can be divided into five categories: Identifier, Keyword, Literals, Operators, Separators.

Identifier – These are the names of variable, method, class, package, interface, etc. These are used to uniquely identify them to the compiler.

Question 29 : Explain instance variable. Give an example

Or,

Define instance variable. Give an example of the same.

Answer : Instance variables are any variable, without “static” field modifier, that are defined within the class body and outside any class’s methods body. It can be declared in class level before or after use. For numbers, the default value is 0 and for boolean , it is false.

Example:

class Book

{ 

   int sub;

       // instance variable

  int price;

     // instance variable

}

Question 30 : State the difference between == operator and equals() method.

Answer : Difference between == and equals () method:

==operator equals() method
== is a relational operator. equals() is method.
== is used to check reference or memory address. equals() is used to compare the contents of the object.
== is operator , so we cannot change the behavior of == operator. equals() method can be overridden and we can define the criteria for the object equality.

Question 31 : If a=5, b=9 calculate the value of a+=a++-++b+a;

Answer : a+ = a ++ – ++ b + a

a = a + (a++ – ++ b + a)

= 6 + (5-10+5) = 6

The value of a after execution will be 6.

Question 32 : Assign the value of pie (i.e., 3.142) to a variable with requisite data type.

Answer : douple pie = 3.142;

Here, for pie (3.142) the suitable data type is double because it can hold 64-bit floating point.

Question 33 : State the difference between = and ==.

Answer : Difference between = and == operator:

= operator ==operator
It is assignment operator. It is relational (binary) operator.
It is used to assign a value to any variable. It compares two objects, based on memory reference.

Question 34 : Write an equivalent Java syntax for the following expression:

a=0.052y3xy

Answer : We can write this statement in Java like:

a = (0.05 – 2 * y * y * y) / (x-y)

Question 35 : Rewrite the following using ternary operator

if(income<=10000)

   tax=0;

else

  tax=12;

Answer : Tax = Income <=10000? 0 : 12;

Question 36 : Write a statement for each of the following

i) Store a number 275 as a string.

ii) Convert the string to a numeric value.

iii) Add it to the existing total of 1000 to update the total.

Answer : Statement for each of the following are:

i) String s = “275”;

ii) int a = Integer.parseInt(s);

iii) total = 1000 + a;

Question 37 : Explain the term type casting.

Answer : Type Casting – It is a term of converting one data type into another.

In Java, two kinds of type casting are used:

i) Implicit Type Casting – It means that a value of one type is changed to a value of another type without any special directive from the programmer. e.g. char to int

ii) Explicit Type Casting – It means the name of the type to which you want a value converted is given, in parenthesis, in front of the value.

e.g.   double d = 5.6;

int k=(int)d;

short s=(short)(d*2.0);

Question 38 : Evaluate the following expressions , if the values of the variables are a=2, b=3 and c=9

i) a-(b++)*(–c)

ii) a*(++b)%c

Answer :

i)  2 – 3 * 8

= 2 – 24

= -22

ii) 2 * 4 % 3

= 8 % 9 = 8

Question 39 : Define a variable.

Answer : A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable’s memory; range of values that can be stored within memory and the set of operations that can be applied to the variable.

Question 40 : What is the purpose of the new operator?

Answer : New operator is used to create an instance of an object. It is also used to create an instance of a class. Actually, it dynamically allocates memory in the heap with the reference, we define pointed from the stack.

Question 41 : State the two kinds of data types.

Answer : Data types are used within type system, which means to identify the type of data and associated operations for handling it.

There are two types of data type:

i) Primitive Data Types byte, float, char, boolean, int.

ii) Composite Data Types class, array, interface.

Question 42 : Find the output of the following program segment, when

i) val=500

ii) val=1600

     int val, sum, n = 550;

     sum = n++val>1750?400:200;

     System.out.println(sum);

Answer : i) When, val = 500, n = 550

Sum = val > 1750?400:200;

= 550 + 500>1750?400:200;

= 1050>1750?400:200;

Sum = 200

ii) When, val=1600, n=550

Sum = n + val>1750?400:200;

= 500+1600>1750?400:200;

= 2150>1750?400:200;

Sum = 400

Question 43 : What will be the output for the following program segment?

int a=0, b=30, c=40;

a=--b + c + ++b;

System.out.println("a="+ a);

Answer : int a = 0, b = 30, c = 40;

a = –b + c + ++b

= 29 + 40 +30

= 99

Question 44 : Mention two different styles of expressing a comment in a program.

Answer : In Java, two different styles of expressing a comment in a program:

i) // for single line comment

ii) /*comment*/ for multiline comment.

Question 45 : Differentiate between operator and expression.

Answer : Difference between operator and expression:

Operator Expression
Operators are special symbols used for mathematical or logical function. An expression is a statement that convey a value.
Generally these are used between two operands. It may be any combination of variables, literals, operators.

Question 46 : If m=5 and n=2, output the values of m and n after execution in (i) and (ii)

i) m-=n;

ii) n=m+

mn

;

Answer : Given, m = 5, n = 2

i) m- = n;

m = m – n

= 5-2

= 3

ii) n = m + (m/n)

= 5 + (5/2)

= 5 + 2.5

= 7.5

Hence, m=3, n=7.5

Question 47 : What will be the output of the following, if x=5 initially?

i) 5*++x;

ii) 5*x++;

Answer : Given, x = 5

i) 5 * ++ x;

5*6 = 30

ii) 5 * x ++;

5*5 = 25

Question 48 : What is the output of the following?

char c='A';

short m=26;

int n=c+m;

System.out.println(n);

Answer : char c=’A’

short m= 26

int n = c + m = 65 + 26 = 91

Output 91

4 Marks Questions

Question 49 : What is the output of the following code?

i)

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);

ii)

char x='A';

int m;

m=(x=='aa' ? 'A' : 'a');

System.out.println("m ="+m);

Answer : i) m = 6, n=14

ii) m = 97