Practice: Questions
Practice: Questions
Practice: Questions
Practice Questions
---------------------------->Objective Type Questions<-------------------------------
Fill in the blanks:
1. Java uses Unicode character set.
2. Unicode is a Two bytes character set.
3. Character Set is a set of valid characters that a language can recognize.
4. The smallest individual unit in a program is a Token.
5. Identifiers are fundamental building blocks that give names to different parts of a
program.
6. Non-graphic characters (or Escape Sequence) are characters that cannot be typed directly
from the keyboard.
7. Variables represent named storage locations whose values can be manipulated during
program execution.
8. Operators that act upon two operands are referred to as Binary operators.
9. The process of converting one predefined type into another is called Type Conversion.
10. The new operator is used to allocate memory for objects and arrays.
----------------------------->Subjective Type Questions<-----------------------------
A. Answer the following questions:
1. What is Character Set?
Ans. Character set is a collection of all the characters that can be used in a Java program. A character
represents any letter (whether capital or small), digits (0 to 9) or any symbol (%, &, ^, @, $, |, etc.).
2. What is bytecode?
Ans. Bytecode is an intermediate language which is received upon compilation of source code and
interpreted by the JVM (Java Virtual Machine).
3. What is indentation in a Java program?
Ans. Indentations are whitespaces that are given by programmers to increase the readability of a
source code.
4. What are keywords?
Ans. Keywords are reserved words in Java and thus provide a special meaning to the Java compiler.
5. What are Identifiers? State the rules while using Identifiers.
Ans. Identifiers are the names given to different parts or elements of a program to identify it.
The rules that you should keep in mind to use identifiers are:
a) It can have any alphabet (capital or small), digits, underscore and dollar sign characters.
b) It should not begin with digits or should not contain any special character.
c) It cannot have a space in between.
d) It must not be a keyword.
e) It can be of any length.
f) Since Java is case-sensitive, it treats capital (upper-case) and small
(lower-case) characters differently.
6. What is a literal? What are the different types of literals available in Java?
Ans. Literals are tokens that are used to represent constants that may be assigned to a variable.
Integer literal or Fixed point literal, Floating point literal, Boolean literal, Character literal, String
literal, Null literal and Class literal are the literals that are used in Java.
8. How are Decimal, Octal and Hexadecimal integer literals represented in Java?
Ans. Decimal Integer Literals use digits from 0 to 9 (i.e. 10 digits)
Octal Integer Literals use digits from 0 to 7 (i.e. 8 digits) and should be prefixed with 0.
Hexadecimal Integer Literals use digits from 0 to 9 and alphabets from A (or a) to F (or f) representing 10
to 15 (i.e. 16 numbers) altogether and should be prefixed with 0x or 0X
9. What are the escape sequences available in Java?
Ans. The escape sequences available in Java are:
Escape Sequences Meaning
'\b' Backspace
'\f' Form feed
'\n' New line
'\r' Carriage return
'\t' Horizontal Tab
'\' Single quote
"\" Double quote
'\\' Backslash
'\0n' Octal number, where n is the number
'\xHn' Hexadecimal number, where n is the number
'\uHn' Unicode character represented through its hexadecimal code n
26. State the difference between Shift right (>>) and Shift right zero fill (>>>)
operators.
Ans. Shift right( >>) operator is used to shift bits towards the right and the vacant places after
shifting is filled up with 0 if the number is positive and 1 if the number is negative to thus retaining
the sign after shifting.
Shift-right-zero-fill shift bits towards the right and the vacant places after shifting is filled up with
0. Thus the number always results to a positive number.
27. What is type conversion? Under what circumstances is there a "Loss of
Information"?
Ans. The process of converting implicitly or explicitly one data type into another compatible data
type is termed as type conversion.
In case a variable or constant of higher size is assigned to a variable of smaller size, it results in
possible loss of precision error.
28. What are comments? What are the different types of comments that may be
used in a Java program?
Ans. Comments are notes in a program to provide an easy understanding of the problem. This
ensures proper documentation. But these statements are ignored by the compiler and no question
of error may occur within this, whatsoever you may write within it. There are three types of
statements available in Java, viz. Single-line Comment, Multiline Comment and Documentation
comment.
29. What is the purpose of new operator?
Ans. The new operator is used to allocate memory for objects and arrays.
36. State one difference between the floating point literals float and double.
Ans. The float data type is of size 32 bits, whereas double data type is of 64 bits.
int a=5,b=6,t;
t=a;
a=b;
b=t;
2. Write a program to display the names of five fruits with a single System.out.println();
statement, but in different lines.
Ans. public class Question2
{
static void main()
{
System.out.println("Apple\nOrange\nGuava\nMango\nGrapes");
}
}
3. Write a program using float type variables to find the area and perimeter of a square
whose side is 12cm.
Ans. public class Question3
{
static void main()
{
float s=12,a;
a=s*s;
System.out.println("Area of the square="+a+" square cm");
}
}
4. Write a program using int variables to find the sum of three numbers, say 15, 36 and 45
and subtract the result from 100.
Ans. public class Question4
{
static void main()
{
int a=15,b=36,c=45,d=100,e,f;
e=a+b+c;
f=d-e;
System.out.println("Sum="+e);
System.out.println("Difference="+f);
}
}
5. Write a program using int variables to find the area and perimeter of a rectangle of
length 12cm and breadth 8cm.
Ans. public class Question5
{
static void main()
{
int length=12,breadth=8,area,perimeter;
area=length*breadth;
perimeter=2*(length+breadth);
System.out.println("Area="+area);
System.out.println("Perimeter="+perimeter);
}
}
6. Write a program using variables to find the profit and profit percent of a certain
transaction where S.P.= `10000 and C.P.= `7000.
Ans. public class Question6
{
static void main()
{
int SP=10000,CP=7000;
double profit,profitPercentage;
profit=SP-CP;
profitPercentage=(double)profit/CP*100;
System.out.println("Profit="+profit);
System.out.println("Perimeter="+profitPercentage);
}
}
7. Write a program using variables to find the cost of 17 pencils if the cost of one pencil= `2.50.
Output should be:
Cost of 1 pencil=`2.5
Cost of 17 pencils=`42.5
Ans. public class Question7
{
static void main()
{
float rate=2.50f, cost;
int pencils=17;
cost=pencils*rate;
System.out.println("Cost of 1 pencil= Rs "+rate);
System.out.println("Cost of 17 pencils= Rs "+cost);
}
}
8. Write a program to initialize three int variables a, b and c with 234, 456 and 712 and
store the sum of the last digits of the variables into d and display it.
Ans. public class Question8
{
static void main()
{
int a=234, b=456, c=712, d;
d=a%10+b%10+c%10;
System.out.println("Sum of last digits="+d);
}
}
9. Write a program to initialize an int variable a with 76498 and from it extract the first
digit and store it in f and extract the last digit in l and display both these digits.
Ans. public class Question9
{
static void main()
{
int a=76498,f,l;
f=a/10000;
l=a%10;
System.out.println("First digit="+f);
System.out.println("Last digit="+l);
}
}
10. Write a program using variables to find the average of 35, 43 and 97.
Ans. public class Question10
{
static void main()
int a=35,b=43,c=97;
float avg;
avg=(float)(a+b+c)/3;
System.out.println("Average of the numbers="+avg);
}
}
3. 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 + m/n;
Ans. i) m=3 and n=
ii) m=5 and n=7-+
11. Write a Java statement that assign the value of pie (i.e. 3.142) to a variable with
requisite data type.
Ans. double pi=3.142;
D. State the value of a, b and c after the execution of each of the following
statement where a=12, b=13 and c=11:
1. a=a++ + – –b + c++
2. b=b++ + ++a * 2
3. c=c++ + a++ * (++b)
4. b=b++ + ++b + c++ + ++a
5. a= --a + b++ + ++a + --b * c++
6. a+=a++ + ++b – c++
7. b+= --c + ++c + a++ + (b++)
8. c+=a-- + --a + (b++) * (++b)
9. a++=a++ + ((++b)/2)
Ans. 1. a=35, b=12, c=12
2. a=13, b=39, c=11
3. a=13, b=14, c=179
4. a=13,b=52,c=12
5. a=179,b=13,c=12
6. a=27,b=14,c=12
7. a=13,b=59,c=11
8. a=10,b=15,c=228
9. a=31,b=14,c=11