Values, Data Types and Operators
Section A
2005
Question 1
(b) Mention two different styles of expressing a comment in a program. [2]
(1) Single line comment: //
(2) Multi-line comment : /*
*/
(d) Differentiate between operator and expression. [2]
Operator is a symbol that do some kind of operation on one or more operands.
E.g.: a = 5. Here a and 5 are operands and = is an operator.
An expression is a valid combination of operators and operands.
E.g: a = 5; Here a and 5 are operands, and = is an operator. The a = 5 is the expression.
(e) If m=5 and n=2 output values of m and n after execution in (i) and (ii):
(i) m- = n
(ii) n= m + m/n [2]
Output Working (i) m=3 m-=n n=2 m=m-n 5–2 3 (ii) m=5 n=m+m/n n=7 5+5/2 5+2 7
Question 3
(a) What will be the output of the following, if x = 5 initially?
(i) 5 * ++x
(ii) 5 * x++ [2]
Output Working (i) 30 5*++x 5*6 30 (ii) 25 5*x++ 5*5 25
(b) What is the output of the following?
char c = 'A';
short m = 26;
int n = c+m;
System.out.println(n); [2]
Output Working 91 n=c+m ‘A’+26 65+26 91
2006
Question 1
(c) Define variable. [2]
Variable represents a named memory location which holds a value of a particular data type that can be manipulated during program run.
E.g.: int a; Here a is the variable.
(e) What is the purpose of the new operator? [2]
The new operator helps to create a new object or a new array at run time; that means it helps to allocate memory for objects or arrays during run time. E.g.:
object creation: Rectangle rect1 = new Rectangle();
array creation: int a[] = new int[10];
Question 2
(a) State the two kinds of data types. [2]
The two kinds of data types are Primitive data types and Reference data types.
(b) Write the corresponding expressions for the following mathematical operations:
(i) a2+b2
(ii) z = x3 + y3 - xy / z [2]
(i) Math.pow(a,2)+Math.pow(b,2)
(ii) double z=Math.pow(x,3) + Math.pow(y,3) – (x*y/z)
(d) 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); [2]
(i) 200
(ii) 400
(f) 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); [2]
Output Working a = 98 a = --b + c++ + b 29 + 40 + 29 69 + 29 98
2007
Question 1
(b) Define Instance Variable. Give an example of the same. [2]
Instance variables are the variables declared for objects and these are declared at class level without static keyword.
Each object gets separate copy of these variables.
E.g.:
public class Rectangle
{
double l,b; //instance variables
}
(d) Assign value of pi (i.e. 3.142) to a variable with requisite data type. [2]
float pi = 3.142f;
Question 2
(c) Explain the term type casting. [2]
Conversion of data type from one type to another type is known as type conversion. The explicit type conversion done
forcefully by the user is known as type casting.
E.g.: (char)65. The integer 65 is converted to char data type, i.e., the character A.
(d) Name the following:-
(i) A package that is invoked by default.
(ii) A keyword, to use the classes defined in a package. [2]
(i) java.lang
(ii) import
(e) Name the class that is used for different mathematical functions. Give an example of a function. [2]
Class name: Math
Function: sqrt()
Example: Math.sqrt(36)
The output is 6.0, the square root of 36.
Question 3
(a) State the difference between = and ==. [2]
The = is an assignment operator which assign a data element to a variable. E.g.: a=5
The == is a relational operator which checks the equality of two primitive data type operands. E.g.: a==5
(b) Write an equivalent Java expression: a=[0.05-2y3] / [x-y] [2]
a=(0.05–2*Math.pow(y,3))/(x-y)
(c) Rewrite the following using Ternary operator:
if(income<=10000)
tax = 0;
else
tax=12; [2]
tax = income<=10000 ? 0 : 12;
(f) What is the output of the following:
(i) System.out.println ("four :"+4+2);
System.out.println ("four :"+(2+2)); [2]
(i) four :42
four :4
(g) 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 [2]
Value of a Working (i) -22 a-(b++)*( - -c) 2–3*8 2–24 -22 (ii) 8 a*(++b)%c 2*4%9 8%9 8
2008
Question 1
(a) Mention any two attributes required for class declaration. [2]
The class keyword and class name.
(b) State the difference between token and identifier. [2]
Token is a smallest unit in a program. There are five tokens in Java. These are keywords,
literals, operators and separators.
Identifiers are one of the tokens. Names given to fundamental building blocks of a program
such as classes, methods, variables, objects, arrays etc are identifiers.
(c) Explain instance variable. Give an example. [2]
Instance variables are the variables declared for objects and these are declared in class level without static keyword.
Each object gets separate copy of these variables.
E.g.:
public class Rectangle
{
double l,b;
}
(d) What is inheritance and how is it useful in Java? [2]
Inheritance is the process by which objects of one class (sub class) acquires the properties of another class (base class). When a class (base class) is defined with instance variables and methods for some objects then these variables and methods can be used for objects of another class (sub class). So memory can be saved and programming effort also can be reduced.
Question 2
(c) If a = 5, b =9 calculate the value of a+ = a++ - ++b +a [2]
Value of a = 6
Working:
a+= a++ - ++b +a
a = a + (a++ - ++b +a)
5 + (5 – 10 + 6)
5 + (-5 +6)
5 + 1
6
(d) Give the output of the following expressions:
(i) If x = -9.99, calculate Math.abs(x);
(ii) If x = 9.0, calculate Math.sqrt(x); [2]
(i) 9.99
(ii) 3.0
2009
Question 1
(b) State the difference between a boolean literal and a character literal. [2]
Boolean literal is a literal with a value either true or false. E.g.: boolean a = false;
Character literal is a single character in single inverted comma. It can be an alphabet or digit or special character. E.g. char c = ‘y’;
(c) What is the use and syntax of a ternary operator? [2]
The ? : is the ternary operator. It is used to select a value against a test expression.
If the test expression is true then the expression after the ? is selected else the expression after the : will be selected.
Syntax: expression1 ? expression2 : expression3;
E.g. : a>b ? a : b;
(d) Write one word answer for the following:
(i) A method that converts a string to a primitive integer data type.
(ii) The default initial value of a boolean variable data type. [2]
(i) Integer.parseInt()
(ii) false
Question 3
(a) Write an expression for (a+b)n /√3 + b [2]
Math.pow(a+b,n)/(Math.sqrt(3)+b)
2010
Question 1
(b) What do you mean by type conversion?
How is implicit conversion different from explicit conversion? [2]
Converting a data type into another data type is known as type conversion.
There are two kinds of type conversions: implicit conversion and explicit conversion.
Implicit conversion: In a mixed data type expression, the expression automatically converted to the largest
data type which is present in it. So the output will be the largest data type.
E.g.: In 5/2.0, the 5 is an int type and 2.0 is a double type value; so the output will be automatically 2.5 which is a
double type value. (In 5/2 the output is 2 because both are int type values).
Explicit conversion: When a programmer forcefully converts a data type into another data type then it is
explicit conversion.
E.g.: In (double)5/2 the output will be 2.5. The reason is that the int type 5 is forcefully converted to double
by the programmer using type caste operator i.e., (double).
Question 2
(b) What is the result stored in x after evaluating the following expression:
int x=5;
x = x++ * 2 + 3 * --x; [2]
Result: 25
Working
x = x++ * 2 + 3 * --x;
= 5 * 2 +3 * 5
= 10 + 15
= 25
Question 3
(b) (ii) What is the output?
char x= ‘A’; int m;
m=(x== ‘a’) ? ‘A’ : ‘a’;
System.out.println("m="+m); [2]
m=97
(g) What will the following functions return when executed?
(i) Math.max(-17, -19)
(ii) Math.ceil(7.8); [2]
(i) -17
(ii) 8.0
2011
Question 1
(b) What does the keyword token refers to, in the context of Java? Give an example for keyword. [2]
The keywords are the words that convey a special meaning to the language compiler. These are reserved for special purpose and must not be used as identifier names.
(e) What is the difference between / and % operators? [2]
The / is division operator that returns quotient when an operand is divided by another one. int x=5/3 returns 1 as the quotient. The % is modulous operator that returns remainder when an operand is divided by another one. int x=5%3 returns 2 as the remainder.
Question 3
(b) What will be the output of the following code?
(i) int k=5,j=0;
k+=k++ - ++j + k;
System.out.println(“k=”+k);
System.out.println(“j=”+j); [2]
Output:
k=15
j=1
Working:
k+=k++ - ++j + k
k=k+(k++ - ++j + k)
=5+(5 – 1 + 6)
=5+(4+6)
=5+10
=15
(ii) double b = -15.6;
double a=Math.rint(Math.abs(b));
System.out.println(“a=”+a); [2]
a=16.0
(e) Write an expression in Java for z=[5x3 + 2y] / [x+y] [2]
z=(5*Math.pow(x,3) + 2*y) / (x+y)
2012
Question 1
(a) Give one example for a primitive data type and a composite data type. [2]
Primitive: int Composite: class
(b) Give one point of difference between unary and binary operators. [2]
Unary: Operators with single operand: E.g.: ++ (++a)
Binary: Operators with two operands: E.g.: = (a=5)
(d) Write Java expression for √(2as + u2) [2]
Math.sqrt(2*a*s + u*u)
Question 3
(a) 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; [2]
x is true
y is 36
(b) State the value n and ch
char c= ‘A’;
int n=c+1;
char ch=(char)n; [2]
n is 66
ch is B
(c) What will be result stored in x after evaluating the following expression?
int x=4;
x+=(x++)+(++x)+x; [2]
x = 20
Working
x+=(x++)+(++x)+x
x=x+((x++)+(++x)+x)
=4+(45+6+6)
=4+16
x=20
(d) 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)) [2]
2.0
3.0
2013
2013
Question 1
(a) What is meant by precedence of operators? [2]
Operator precedence refers to the order of evaluation of operators in an expression with various operators. For example in the expression a=6+8/2, higher precedence is for / operator; next precedence is for + operator; and last precedence is for =.
Question 1
(b) What is literal? [2]
Literals are data items that never change their value during program run. It is also known as constants.
(e) 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; [2]
(i) Type casting. The double value of x (15.2) is converted to int value.
(ii) Type promotion. The int value of x (12) is converted to long value.
Question 3
(f) Write a java expression for ut+1/2ft2 [2]
u*t + 1/2.0 * f * Math.pow(t,2)
2014
Question 1
(a) Which of the following are valid comments?
(i) /* comment */
(ii) /* comment
(iii) // comment
(iv) */ comment */ [2]
(i) /* comment */
(iii) // comment
(c) Name the primitive data type in Java that is:
(i) a 64 integer and is used when you need a range of values wider than those provided by int.
(ii) a single 16 bit Unicode character whose default is ‘\u0000’. [2]
long
null character
(d) State one difference between the floating point literals float and double. [2]
float: It requires 4 bytes memory to store a value.
double: It requires 8 bytes memory.
Question 2
(a) Operators with higher precedence are evaluated before operators with relateively lower precedence.
Arrange the operators given below in order of higher precedence to lower precedence.
(i) &&
(ii) %
(iii) >=
(iv) ++ [2]
++
%
>=
&&
(b) Identify the statements listed below as assignment, increment, method invocation or object creation statements.
(i) System.out.println("Java");
(ii) costPrice = 457.50;
(iii) Car hybrid = new Car();
(iv) petrolPrice++; [2]
(i) method invocation
(ii) assignment
(iii) object creation
(iv) increment
Question 3
(c) What are the final values stored in variables x and y below?
double a = -6.35; double b = 14.74;
double x = Math.abs(Math.ceil(a));
double y = Math.rint(Math.max(a,b)); [2]
Value of x=6.0
Value of y=15.0
(e) Give the output of the following method:
public static void main(String args[]) {
int a=5;
a++ ;
System.out.println(a);
a-=(a--) – (--a);
System.out.println(a); } [2]
Output:
6
4
Working
a-= (a--) – (--a)
a = a – ( (a--) – (--a))
= 6 – (6 – 4)
= 4
2015
Question 1
(a) What are the default values of the primitive data type int and float? [2]
There are no default values for primitive data types. The values for default initialization for int and float are 0 and 0F or 0.0F
(c) What are identifiers? [2]
Identifiers are the names given to the fundamental building blocks of a program that are classes, methods, variables, objects, arrays, interface, packages etc. E.g.: The Sum in class Sum
(d) Identifiy the literals listed below:
(i) 0.5
(ii) ‘A’
(iii) false
(iv) “a” [2]
(i) Real literal / Floating point literal
(ii) Character literal
(iii) Boolean literal
(iv) String literal
Question 2
(a) Evaluate the value of n if the value of p = 5, q = 19
int n = (q-p)>(p-q) ? (q-p) : (p-q); [2]
14
(b) Arrange the following primitve data types in ascending order of their size:
(i) char
(ii) byte
(iii) double
(iv) int [2]
byte, char, int, double
(c) What is the value stored in variable res given below:
double res = Math.pow(“345”.indexOf(‘5’),3); [2]
8.0
Question 3
(e) Name the mathematical function which is used to find sine of an angle given in radians? [2]
Math.sin()
(i) Write Java expressions for
[a2 + b2] / 2ab [2]
(Math.pow(a,2)+Math.pow(b,2)) / (2*a*b)
(j) If int y=10, then find int z=(++y * (y++ + 5); [2]
176
2016
Question 1
(b) What are keywords? Give an example. [2]
Keywords are words that convey special meaning to the language compiler. They are reserved for special purpose and must not be used as identifiers. E.g.: class int etc.
Question 2
(b) What are the types of casting shown by the following examples:
(i) char c=(char)120;
(ii) int x=’t’; [2]
(i) Explicit type conversion. It type casts the integer 120 to a character value, which is ‘x’.
(ii) Implicit type conversion. It type promotes from character ‘t’ to int type value that is 116.
Question 3
(b) Give the output of the following Math functions:
(i) Math.ceil(4.2)
(ii) Math.abs(-4) [2]
(i) 5.0
(ii) 4
(i) Give the output of the following expression:
a+=a++ + ++a + --a + a--; when a=7 [2]
a=39
a+=a++ + ++a + --a + a--
a=a+( a++ + ++a + --a + a--)
=7+(7+9+8+8)
a=39
2017
Question 1
(b) Name the operators listed below:
(i) <
(ii) ++
(iii) &&
(iv) ? : [2]
(i) Less than (Relational)
(ii) Increment
(iii) Boolean Logical And
(iv) Conditional:
(c) State the number of bytes occupied by char and int data types. [2]
char Two bytes
int Four bytes
(d) Write one difference between / and % operator. [2]
The / is division operator and it is used to find quotient.
The % is modulous operator and it is used to find remainder.
Question 2
(a) Name the following:
(ii) Any one reference data type [2]
(ii) class
(e) Write the output:
char ch=‘F’;
int m=ch;
m=m+5;
System.out.println(m+" "+ch); [2]
75 F
Question 3
(a) Write a Java expression for the following:
ax5+bx3+c [2]
a*Math.pow(x,5) + b*Math.pow(x,2) + c
(b) What is the value of x1 if x=5?
x1=++x - x++ + - -x [2]
6
2018
Question 1
(c) Classify the following as primitive or non-primitive data types:
(i) char
(ii) arrays
(iii) int
(iv) classes [2]
Primitive data types: (i) char (iii) int
Non-primitive data types: (ii) arrays (iv) classses
Question 2
(c) (i) int res = ‘A’;
What is the value of res? [2]
(i) 65
(d) Write the return data type of the following.
(i) endsWith()
(ii) log() [2]
(i) boolean
(ii) double
Question 3
(f) Write a Java expression for the following:
√(3x+x2 )/(a+b) [2]
Math.sqrt(3*x+Math.pow(x,2))/(a+b)
(g) What is the value of y after evaluating the expression given below?
y+= ++y + y-- + --y; when int y = 8 [2]
33
(h) Give the output of the following:
(i) Math.floor(-4.7)
(ii) Math.ceil(3.4) + Math.pow(2,3) [2]
(i) -5.0
(ii) 12.0
(j) Write the output of the following:
System.out.println(“Incredible”+”\n”+”India”); [2]
Incredible
India
(i) Rewrite the following using ternary operator:
if(bill>10000)
discount=bill*10.0/100;
else
discount=bill*5.0/100; [2]
discount=(bill>10000)? bill*10.0/100 : bill*5.0/100;
2019
Question 1
(b) Write a difference between unary and binary operator. [2]
Operator that operates on one operand is known as unary operator. E.g.: ++, --
Operator that operates on two operands is known as binary operator. E.g.: = + etc
(d) Write memory capacity of short and float data type in bytes [2]
short: 2 bytes, float: 4 bytes
(e) Identify and name the following tokens:
(i) public
(ii) ‘a’
(iii) ==
(iv) { } [2]
public: Keyword
‘a’: Literal
== operator
{ } Punctuator
Question 2
(b) Give output:
String P=”20”, Q=”19”;
int a=Integer.parseInt(P);
int b =Integer.parseInt(P);
System.out.println(a+""+b); [2]
2020
Question 3
(a) Write a Java expression for the following:
|x2+2xy| [2]
Math.abs(x*x+2*x*y)
(b) Write return type of the following functions:
(ii) random() [2]
(ii) double
(c) If the basic=1500, what will be the value of tax after the following is executed?
tax=basic>1200?200:100; [2]
200
(f) Give output:
Math.sqrt(Math.max(9,16)); [2]
4
(h) Evaluate the following expression if the value of x=2,y=3 and z=1:
v=x+--z+y+++y [2]
9
2020
Question 1
(c) Name the following:
(i) The keyword which converts variable into constant.
(ii) The method which terminates the entire program from any stage. [2]
(i) final
(ii) System.exit()
(d) Which of the following are primitive data types?
(i) double
(ii) String
(iii) char
(iv) Integer [2]
(i) double
(iii) char
(e) What is an operator? Name any two types of operators used in Java. [2]
Operators are symbols that do some kind of operations (actions) on one or more operands.
E.g.: a = 5. Here = is an operator.
= Assignment operator
+ - * / % Arithmetical Operators
Question 3
(a) Write a Java expression for the following:
√(b2-4ac) [2]
Math.sqrt(Math.pow(b,2)*a*c)
(b) Evaluate the following if the value of x=7, y=5
x+=x++ + x + ++y [2]
Output Working (b) x=28 x+=x++ + x + ++y x=x+(x++ + x + ++y) 7+(7 + 8 + 6) 7 + 21 28
(d) Write the output of the following statement:
System.out.println("A picture is worth \t \"A thousand words.\""); [2]
A picture is worth "A thousand words."
(g) Rewrite the following program segment using logical operators:
if ( x > 5 )
if ( x > y )
System.out.println(x+y); [2]
if(x>5&&x>y)
System.out.println(x+y);
(i) Give the output of the following:
(i) Math.pow (36,0.5) + Math.cbrt (125)
(ii) Math.ceil (4.2 ) + Math.floor (7.9) [2]
(i) 11
(ii)12.0
(j) Rewrite the following using ternary operator:
if(n1>n2)
r = true;
else
r = false [2]
r=n1>n2?true:false;