Professional Documents
Culture Documents
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.
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
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.
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
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
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
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
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 .
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