You are on page 1of 12

CHAPTER : 4

Topic : VALUES AND DATA TYPES


Questions & Answers
A. Choose the correct answer from the following options :
1. ASCII stands for _______________.
a) American Standard Code for Information Interchange.
b) American Simulated Code for Information Interchange.
c) American Standard Code for Interchange of Information.
d) American Standard Code for Interaction of Information.
Answer : a) American Standard Code for Information Interchange.
2. ASCII is ___________.
a) 6 – bit set of codes b) 7– bit set of codes
c) 8 – bit set of codes d) 16 – bit set of codes
Answer : b) 7– bit set of codes
3. Extended ASCII is ___________.
a) 6 – bit set of codes b) 7– bit set of codes
c) 8 – bit set of codes d) 16 – bit set of codes
Answer : c) 8– bit set of codes
4. Which of the following is not a token ?
a) Keywords b) Operators c) Identifiers d) Procedure
Answer : d) Procedure
5. Which of the following is a keyword ?
a) character b) object c) break d) attribute
Answer : c) break
6. Which of the following is not legal identifier ?
a) _age b) 9thClass c) $Amount d) null value
Answer : b) 9thClass
7. Which of the following is an invalid integer ?
a) 1999 b) 199999 c) 199 99 d) 199
Answer : c) 199 99
8. Which of the following is not a character literal ?
a) ’\t’ b) ’t’ c) ”t” d) All of these
Answer : c) ”t”
9. Which of the following is not a primitive data types in Java?
a) boolean b) short c) float d) class
Answer : d) class
10. Which of the following punctuator is the statement terminator in Java ?
a) ; b) , c) . D) All of these
Answer : a) ;
11. Which of the following is the size of a long data type in Java ?
a) 32 bits b) 64 bits c) 48 bits d) long data type is not supported in Java
Answer : b) 64 bits
12. Which of the following is the size of a boolean data type in Java ?
a) 1 bits b) 8 bits c) 16 bits d) Boolean data type does not take any space
in memory.
Answer : b) 8 bits
13. There are ________ different types of token in Java.
a) 7 b) 5 c) 6 d) 8
Answer : b) 5
14. Which of the following is the size of a char data type in Java ?
a) 16 bits b) 8 bits c) 32 bits d) 64 bits
Answer : a) 16 bits
15. The _______ keyword is used to declare a constant in Java.
a) public b) final c) char d) int
Answer : b) final
B. State whether given statements are True or False :
1. Java supports the use of the ASCII character set only . FALSE
( Java supports the use of the Unicode character set only)
2. The smallest unit in a Java programs is known as token. TRUE
3. The Unicode character set uses 16 bits per character. TRUE
4. In an escape sequence , a character is a preceded by a forward slash(/). FALSE
( In an escape sequence , a character is a preceded by a backward slash (\).
5. In Java, an identifier can begin with a $ sign . TRUE
6. Java offers five types of tokens. TRUE
7. The char data type reserves 8 bits in memory. FALSE
( The boolean data type reserves 8 bits in memory. )
8. The boolean data types are used for storing logical values. TRUE
9. If a literal constant contains a decimal point , then it is of the type double by default. TRUE
10. The modifier final can be can be applied to a variable declaration to ensure that the values stored in the variable
can not be changed after the variable has been initialised . TRUE

C. Answer the following questions :


1. What is a character set ?
Answer : Character Set : A character set is a defined list of characters recognised by the computer hardware and
software .
2. What is an escape sequence in Java ?
Answer : Escape Sequence Character : An escape sequence is a set of character that has a special meaning for the
Java compiler . In an escape sequence , a cheater is followed by a backslash(\). Some examples of escape
sequences are ‘\n’ and ’\t’.
3. int res = ‘A’ ; What is the value of res ?
Answer : 65
4. Write the output for the following :
System.out.println(“Incredible “+”\n”+”World”);
Answer : Incredible
World
5. What are Token in Java ? Name any three tokens.

Answer : Token : All characters in a Java program are grouped into symbols called tokens . A computer program
consists of set of instructions called statements . A statement is composed of various components . Each
individual component is called token in Java . Keywords, identifiers and literals are three different tokens in Java.

6.What are keywords ? Give an example.


Answer : Keywords : Keywords are reserved words because the Java compiler reserves these words for its own
use and therefore are not available as names for variables or methods They have a special meaning to the Java
compiler .For example void and public .

7. What are Identifiers ?


Answer : Identifiers : Identifiers are used to name different parts of a program such as variables , methods,
classes , objects etc.

8. What is a literals ?
Answer : Literals : Literals are a sequence of character that represent values in a program and are stored in a
variable. They consist of digits , letters and other characters.

9. Identify literals listed below : i) 0.5 ii)’A’ iii) False iv) “a”
Answer : i) 0.5 - Real ii)’A’ – char iii) False – boolean iv) “a” – String

10. What are primitive data type ? Name eight primitive data types in Java.
Answer : Primitive data types are fundamental data type that are an integral part of Java language . They are
also known as intrinsic data type. The names of eight primitive data types are as follows : byte, short , int , long,
float , double , char and boolean.
11 Arrange the following primitive datatypes in the ascending order of their size.
i) char ii) byte iii) double iv) int
Answer : i) byte ii) char iii) int iv ) double

12. State the number of bytes occupied by char and int data types .
Answer : The char data type occupies 2 bytes and the int data type occupies 4 bytes .
13. State the difference between a boolean literal and a character literal .
Answer : The difference between a boolean literal and a character literal are as follows :
boolean literal character literal

1. A boolean literal is always of boolean 1. A character literal is of char type .


type . It’s either boolean value true or It is one character enclosed in single
false . quotes . Example :’G’.
2. It uses 1 byte of storage. 2. It uses 2 bytes of storage.

14. What are default values of the primitive data type int and float ?
Answer : The default value of int is 0 and float is 0.0 f.

15. Name the primitive data type in Java :


i) A 64 bit 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 value is ‘\u0000’.
Answer : i) long and ii) char

16. State one difference between floating point literal and double literal
Answer : Floating point literal require 4 bytes whereas double literal require 8 bytes.
17. Give one example each of a primitive data type and a composite data type.
Answer : Primitive data type : int and float.
Composite data type : class and array

18. Describe the use of the final keyword.


Answer : The modifier final can be applied to a variable declaration to ensure that value stores in the variable can
not changed after the variable has been initialised.
19. Classify the following as primitive or non- primitive data types.
i) char ii) arrays iii) int iv) classes
Answer : i) char - Primitive data type ii) arrays – Non – Primitive Data type
iii) int - Primitive data type iv) classes - Non – Primitive Data type
20. Give the difference between char and String
Answer : The difference between String and char are as follows :
String char
1. It is a reference data type. 1. It is a primitive data type.
2. It is a collection of characters, 2. It represents single character
spaces, numbers and special enclosed within a single quotes .
characters enclosed within a double
quotes.
Example String s1 = “ Computer”. Example : char ch=‘M’;
D. Give the answer of the following questions :
1 . Java uses___________ character set . ( Answer : Unicode )
2. Smallest individual unit in a program is known as_______ ( Answer : Token).
E. Choose the correct answer from the following options :
1. Which of the following is not a token ?
a) Keywords b) Identifiers c) Statements d) Operators
( Answer : c) Statements)
2. Identify the illegal identifier from the following ?
a) _CHK b) 20_TO _50 c) $student_name d) A_to_z
( Answer : b) 20_TO_ 50 )
3. Which of the following is not an integral data type in Java ?
a) short b) long c) byte d) double
( Answer : d) double)
4. Which of the following is a primitive data type in Java ?
a) array b) class c) byte d) Character
Answer : c) byte)
5. Which of the following is the default initial value for a long data type ?
a) 0 b) long 0 c) 0L d) All of these
Answer : c) 0L )
6. Which keyward turns a variable declaration into constant declaration ?
a) const b) final c) constant d) fixed
Answer : b) final )
CHAPTER : 5
Topic : OPERATORS IN JAVA
Questions & Answers
A. Choose the correct answer from the following :
1. An operator Taking only single operand for its operations is called __________.
a) A Unary operator b) A Ternary operator c) A Binary operator d) None of these
( Answer : a) A Unary operator )
2. Which one of the following is not a Binary operator ?
a) AND b) == c) % d) !
( Answer : d) ! )
3. Which one of the following is not a valid operator in Java ?
a) < = b) ! == c) ! = d) ==
( Answer : b) ! == )
4. The statement y=y+1 is equivalent to
a) y++ b) ++y c) y+=1 d) All of these
( Answer : d) All of these )
5. For x = 5, the statement sum= ++x + 8 evaluates to
a) sum = 13 b) sum= 14 c) sum= 15 d) sum= 16
( Answer : b) sum= 14 )
6. The statement ( 1>0) && (1<0 ) evaluates to
a) 0 b) 1 c) false d) true
( Answer : c) false )
7 . The statement ( 1>0) || (1<0 ) evaluates to
a ) 0 b) 1 c) false d) true
( Answer : d) true )
8. The statement ( 1==1 ) ? 1: 0 evaluates
a) 0 b) 1 c) false d) true
( Answer : b) 1 )
9. The expression 13 % 3 gives the output
a) 4 b) 3 c) 2 d) 1
( Answer : d) 1 )
10 . The expression 13/ 3 gives the output
a) 4 b) 3 c) 2 d) 1
( Answer : a) 4 )
11. The System.out.println(“Six”+3+3); gives the output
a) Six 33 b) 33 Six c) Six 6 d) 6 six
( Answer : a) Six 33 )
12. The expression 4 + 8 % 2 gives the output : a) 4 b) 8 c) 6 d) 1
( Answer : a) 4 )
B. State whether the gives statements are True or False :
1. Arithmetic operators + and - also have unary form. TRUE
2. Operators = and == perform the same operation in Java. FALSE
(Operators = performs assignment and == performs equality operation in Java. )
3. The expression 10 % 4 evaluates to 2. TRUE
4. The expression 4/3 evaluates to 1. TRUE
5. The expression 3+4 and “3”+”4” evaluates to the same values . FALSE
(The expression 3+4 and “3”+”4” evaluates to the different values .
6. The expression x = x+7 is same as x=+7. FALSE
( The expression x = x+7 is same as x+=7. )
7. The new operator allocates the memory during runtime . TRUE

8. The statements x=7 and x==7 are same . FALSE


(The statements x=7 and x==7 are different . )
9. The expression z=-7 is same as z = z - 7. FALSE
( The expression z - = 7 is same as z = z - 7.
C. Answer the following questions :
1. What is the result stored inx after evaluating the following expression ?
int x= 5; x = x ++ * 2 + 3 * -- x ;
Answer : int x= 5 6 5
x = x ++ * 2 + 3 * -- x ;
= 5*2 + 3*5
= 10 + 15
x = 25
2. What is the value of a1 if a =5 ? a1 = ++ a – a++ + -- a;
Answer : a1 = ++ a – a++ + -- a; ( a = 5 6 7 6 )
=6– 6 +6
a1 = 6

3. What will be the result stored in x after evaluating the following expression ?
int x = 4 ; x = ( x++) + (++x) + x ;
Answer : x = ( x++) + (++x) + x ; ( x = 4 5 6 )
= 4 +6 +6
x = 16
4. What is the value of y after evaluating the expression given below ?
y = ++ y + y-- + --y ; when y int y = 8;
Answer : y = ++ y + y-- + --y ; ( y = 8 9 8 7 )
= 9 +9+7
= 25

5. Write one difference between / and % operator .


Answer : The division operator (/) divides the first operand by second operand and gives the quotient .
The modulus (%) operator gives the remainder only of the division operator.
Example Division (/) operator : 14 / 7 = 2
modulus (%) operator : 7 % 2 =1

6. Differentiate between '=' and '=='.


Answer : The difference between '=' and '==' are as follows :
'=' '=='
1. It is an assignment operator. 1. It is a relational operator.
2. It is used to assign 2. It gives equality.
value to a variable.
Example : int num=12; Example : int x=5, y=5;
Here 12 value is assign x==y
to the variable num. Here '==' gives equality

7. Distinguish between unary , binary and ternary operators with examples for each of them :
Answer : i ) A unary operator requires only one operand to operate on.
Example : ++ and --
ii) A binary operator requires two operands to operate on .
Example : * and %
iii) A ternary operator requires three operands to operate on .
Example : Conditional operator ? :
8. Name the operators listed below : a) < b) ++ c) && d) ? :
Answer : a) < : Less Than ( Relational Operator )
b) ++ : Increment Operator ( Unary Operator )
c) && : Logical AND operator
d) ? : Ternary Operator
9. What is meant by precedence of operators ?
Answer : Precedence of operators refers to the order in which the operators are applied to the operands in an
expression. For Example / Has higher precedence then + . Therefore , the expression 10 + 10 /5 will
evaluates to 10 +2 = 12.
10. What are the types of casting shown by the following examples ?
a) double x = 15.2 ; int y = (int) x;
b) int x= 12 ; long y = x ;
Answer : a) Explicit Typecasting
b) Implicit Typecasting
11. What are the types of casting shown by the following examples :
a) char ch = ( char ) 120 ; b) int x = ‘b’ ;
Answer : a) Explicit Typecasting b) implicit Typecasting

12. Rewrite the following using ternary operator :


if ( x % 2 == 0)
System.out.print( “ EVEN NO“);
else
System.out.print( “ ODD NO“);
Answer : System.out.println( x%2 == 0 ? “EVEN NO “:“ODD NO” );

13. What will be the output of the following program snippet if X=5 and Y=10?
Y = ( X>=Y ) ? ++ X : ++ Y ;
System.out.pritnln(“ The value of =”+Y);
Answer : X=5 and Y=10 (Given)
5>=10 ( Condition is false)
The output is : The value of = (10+1) =11

14. Rewrite the following code using the ternary operator :


int y;
if( y>=150)
y = y * 15;
else
y = y - 15;
Answer : y = y>=150 ? y * 15 : y - 15 ;
CHAPTER : 7
Topic : INPUT IN JAVA
( Questions & Answers )
A. Multiple choice questions :
1. Single line comments can be added using _______________
a) // b) \\ c) \*….*/ d) None of these
( Answer : a) // )
2. Which keyword do you use to include a class in your program ?
a) include b) import c) export d) impart
( Answer : b) import )
3. Default delimiter used in the Scanner class is __________________.
a) comma b) Colon c) White space d) There is no default delimiter
( Answer : c) White space)
4. Which package would you import to display the date and time ?
a) java.io b) java.lang c) java . date d) java.util
( Answer : d) java.util )
5. Which package would you import for the Scanner class
a) java.io.*; b) java.lang.* ; c) java. util.*; d) java.awt.*;
( Answer : c) java. util.*;)
6. Errors occur in a program when ________________.
a) Syntax of the programming language is not followed
b) The program does not run properly or does not execute at all
c) The program produces an incorrect result
d) All of these
( Answer : d) All of these )
B. Answer the following questions :
1. Name the following :
a) A keyword used to calla package in the program.
b) Anyone reference data type .
Answer a) import b) Array
2. Name any two library packages
Answer : The two library packages are as follows a) java.util and b) java. lang.
3. State the package that contains the Scanner class.
Answer : java.util
4. What is meant by package ? Name any two Java Application Programming Interface Package .
Answer : Package : A package is a named collection of Java classes that are grouped on the basis of their
functionality. Two Java Application Programming Interface Package are java.util and java.lang.
5. What is the use of import keyword ?
Answer : The import keyword is used to include a package or class in a program.
6. Write the Java program code to include all the classes present in the java.util package into your program.
Answer : import java.util.*;

7. What is the difference between the Scanner class next() and nextLine ()?
Answer : The next() method is used to read the next complete token from the Scanner object where as the
nextLine() method is used to read a complete line of text.
8. Name the type of error ( syntax, runtime or logical error ) in each case given below :
a) Math.sqrt(36 -45) b) int a;b;c;
Answer : a) Runtime error b) Syntax error
9. Name the method of Scanner class that :
a) Is used to input an integer data from the standard input device.
b) In used to input a string data from the standard input device .
Answer : a) nextInt() b) nextLine() or next()
10. Name the method of the Scanner class that :
a) Checks if the Scanner has another token in its input.
b) Checks if the Scanner has another line in its input.
Answer : a) hasNext() b) hasNextLine()
11. Name the type of error(syntax , runtime or logical error) in each case given below :
a) Division by a variable that contains a value zero.
b) Multiplication operator used when the operation should be division.
c) Missing Semicolon
Answer : a) Runtime error b) Logical error c) Syntax error

12. Complete the code given below to create an object of the Scanner class .

Scanner sc = ________ Scanner (__________);


Answer : Scanner sc = new Scanner ( System.in );
CHAPTER : 8
Topic : Mathematical library function
Questions & Answers
A. Multiple choice questions:
1. Which java package includes the Math class ?
a) java.io b) java.util c) java.lang d) java.sys
( Answer : c) java.lang )
2. Give the output of Math.sqrt(x); when x=9.0
a) 3 b) 3.00 c) 3.0 d) All of these
( Answer : c) 3.0 )
3. Give the output of Math.ceil(46.6).
a) 46.6 b) 47.0 c) 46.5 d) 46.0
( Answer : b) 47.0 )
4. Give the output of of Math.abs(x); when x= -9.99.
a) -9.99 b) 0 .99 c) 9.99 d) -9.55
( Answer : c) 9.99 )
5. Which of the following is method to find the square root of a number ?
a) FindSquareroot(x) b) Math.Square(x) c) Sqrt(x) d) Math. Sqrt(x)
( Answer : d) Math. Sqrt(x) )
6. Give the output of Math.pow(2,0).
a) 0.0 b) 1 .0 c) 2.0 d) -1.0
( Answer : b) 1 .0
7. Give the output of Math.floor (46.6).
a) 46.5 b) -46.0 c) 47.0 d) 46.0
( Answer : d) 46.0 )
8. Math random() returns a double value r such that
a) 0.0 <= r <1.0 b) 0.0 < r <= 1.0 c) 0.0 <= r < =1.0 d) 0.0 < r <1.0
( Answer : a) 0.0 <= r <1.0 )

B. Answer the following questions:


1. Give the output of the following Math functions :
i) Math.floor(-4.7) ii) Math.ceil(3.4)+Math.pow(2,3)
Answer : i) Math.floor(-4.7) = - 5.0
ii) Math.ceil(3.4)+Math.pow(2,3) = 4.0 +8.0 = 12.0

2. What are the values stored in variables r1 and r2


i) double r1= Math.abs( Math.min(-2.83, -5.83));
ii) double r2 = Math.sqrt(Math.floor(16.3));
Answer : i) double r1= Math.abs( Math.min(-2.83, -5.83)) = 5.83
ii) double r2 = Math.sqrt(Math.floor(16.3)) = 4.0

3. Give the output of the following math functions : i) Math.ceil(4.2) ii) Math.abs(-4)
Answer : i) Math.ceil(4.2) = 5.0
ii) Math.abs(-4) = 4
4. What will the following functions return when executed ?
i) Math.max ( - 17, -19 ) ii) Math.ceil(7.8)
Answer : i) Math.max ( - 17, -19 ) = - 17
ii) Math.ceil(7.8)=8.0
5. Give the output of the following Math function :
Math.round( Math. abs(-2.5) )
Answer : Math.round( Math. abs(-2.5) = 3

6. 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 ));
Answer : double x = Math.abs ( Math.ceil(a)) = 6.0
double y = Math.rint ( Math. Max(a,b )) = 15.0

A. Choose the correct answer from the following options :


1. Give the output of Math.abs(x); when x= -9.99.
a) -9.99 b) 0 .99 c) 9.99 d) -9.55
( Answer : c) 9.99 )
2. Which java package includes the Math class ?
a) java.io b) java.util c) java.lang d) java.sys
( Answer : c) java.lang )
3. Give the output of Math.sqrt(x); when x=9.0
a) 3 b) 3.00 c) 3.0 d) All of these
(Answer : c) 3.0 )

4. Give the output of Math.ceil(47.6).


a) 46.6 b) 48.0 c) 46.5 d) 46.0
(Answer : b) 48.0 )

5. Give the output of Math.floor (46.6).


a) 46.5 b) -46.0 c) 47.0 d) 46. 0
(Answer : d) 46. 0 )
6. Which of the following is a method to find the square root of a number ?
a) FindSquareroot(x) b) Math.Square(x) c) Math.sqrt(x) d) Sqrt (x)
(Answer : c) Math.sqrt(x) )

7. Give the output of Math . pow (2,0)


a) 0.0 b) 2.0 c) 1.0 d) -1 . 0
( Answer : c) 1.0 )
8. Math . random ( ) returns a double value r such that :
a) 0.0 <= r <1.0 b) 0.0 <= r <= 1. 0 c) 0.0 < r <=1.0 d) 0.0 < r <1.0
( Answer : a) 0.0 < = r < 1.0 )

B. Answer the following questions :


1. Give the output of the following Math functions :
a) Math.floor(-4.7 ) b) Math .ceil(3.4) + Math.pow( 2 , 3 )
Answer : a ) -5.0 b) ( = 4.0 + 8.0 ) = 12.0

2. Give the output of the following functions :


a) Math.ceil( 4.2); b) Math.abs (-4);
Answer : a) 5.0 b) 4
3. What will the following functions return when executed ?
a) Math.max(-17, -19) ; b) Math .floor (7.8);
Answer : a) -17 b) 7.0
4. Give the output of the following functions :
Math.round ( Math .abs(-2.5));
Answer : 3
5. What is the value stored in variable r1 ?
double r1 = Math.abs (Math.min(-2.83 , -5.83 ) );
Answer : 5.83
6. 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));
Answer : double x = Math.abs(Math.ceil(a)); x=6.0
double y = Math.rint(Math.max( a,b)); y= 15.0
7. Give the output of the following Math Function : Math.ceil (Math.abs(-2.5));
Answer : 3.0
8. Write down the Java expression for for the following
−𝑏+√𝑏2 −4𝑎𝑐
a) 𝑥 = 2𝑎
b) 𝐴 = 𝜋𝑟 2 + 2 𝜋 rh
c) V = 4/3 𝜋r3
Answer : a) double x = (-b + Math.sqrt(b*b - 4*a*c) )/(2 *a);
or double x =(- b + Math.sqrt(Math.pow(b,2) - 4*a*c) )/(2*a) ;
b) double A = Math.PI * Math.pow(r,2) + 2 * Math.PI * r * h;
c) double V = (4/3 ) * Math . PI * Math.pow(r,3);
9. Distinguish between Math.ceil() and Math .floor() methods .
Answer: The difference between Math.ceil() and Math .floor() are as follows :
Math.ceil() : The Math.ceil(x) method returns the closest integer that is greater than or equal to the argument.
Syntax : Math.ceil(x)
Argument type : double x
Return type : double
Example : Math. ceil(46.6) = 47.0
Math. floor() : The Math. floor(x) method returns the closest integer that is less than or equal to the argument.
Syntax : Math. floor(x)
Argument type : double x
Return type : double
Example : Math. floor(46.6) = 46.0

10. Explain the following Math class functions in Java :


a) Math.sqrt() b) Math.cbrt ()
Answer : a) Math.sqrt() : The Math.sqrt(x) method returns the square root of a double value .
Syntax : Math.sqrt(x);
Argument Type : double x
Return Type : double
Example : Math.sqrt(64) =8.0
b) Math.cbrt () : The Math.cbrt(x) metho returns the cube root of a double value.
Syntax : Math.cbrt(x);
Argument Type : double x
Return Type : double
Example : Math.cbrt(125) = 5.0
11. 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))
Answer : System.out.println( Math.min(Math .floor(x),y)) ;
=2.0
System.out.println( Math.max(Math.ceil(x),y))
= 3.0

You might also like