Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practice: Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Chapter 15: Java Fundamentals

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.

7. State the difference between a Boolean literal and a character literal.


Ans. A character literal is used to enclose a character within single quotes and boolean literal refer
to two states, i.e., true (to represent success) and false (to represent failure).

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

10. What are the different punctuators available in Java?


Ans. There are 9 punctuators available in Java. They are: ( ) { } [ ]; , .
11. What are Tokens?
Ans. Token is the smallest fundamental unit in a program. They are the valid characters or
symbols that can be used in a Java program.
12. State the difference between token and identifier.
Ans. Token is the smallest fundamental unit in a program, whereas identifier is a token which is
used to give names to different parts or elements of a program to identify it.
13. State the two kinds of data types.
Ans. The two data types are:
a) Fundamental Data Type
b) Composite Data Type
14. State the size of integer data types in Java.
Ans. The size of integer data types are:
a) byte – 8 bits
b) short – 16 bits
c) int – 32 bits
d) long – 64 bits
15. How are floating point numbers represented in Java?
Ans. Floating point numbers are used to represent fractional numbers (with decimal point). Java
provides two ways of representing fractional numbers viz. float and double. Float type numbers
are suffixed with F or f and double type numbers are suffixed with D or d, however suffixing
with D or d for double data type is optional.

16. What are variables? How are variables initialized?


Ans. Variable is a name given to memory location where data is stored or needs to be stored.
The general format of variable declaration is as follows:
<data-type><variable-name>;
For initialization of variables use the general syntax:
<data-type> <variable-name>=<constant>;

17. How are symbolic constants represented in Java?


Ans. Symbolic constants are represented with the final keyword before a variable declaration: For
example, final int MAX=100; declares a symbolic constant MAX with the constant value 100.

18. How are negative numbers represented in Java?


Ans. Negative numbers are represented in Java with the – (minus) sign preceding the number.
Example -34, -24.34, etc.
19. Differentiate between operator and expression.
Ans. An operator is a symbol or command that is used to perform a particular task like
evaluating an expression. Expression on the other hand is a combination of an operand and
operators that evaluates to a value.
20. Explain the term typecasting.
Ans. Typecasting is the process of converting the resultant data type of an expression from one
data type to another.
21. State the difference between Unary and Binary Operators?
Ans. Unary operators are mathematical operators that works on a single operand (e.g. –b, ++c )
whereas binary operators are mathematical operators that works on two operands (e.g. a+b, c*d).

22. What is meant by precedence of operators?


Ans. Precedence of operators refers to the order in which a mathematical expression with
multiple mathematical operators gets evaluated.

23. What are relational operators?


Ans. Relational Operators are used to define a conditional expression.
24. What is a compound statement? Give an example.
Ans. The group of statements within a block is referred to as a compound statement.
For example,
{
int a=5, b=6;
System.out.println(a+ “ ”+b);
}
25. What are the different logical operators available in Java?
Ans. The different logical operators available in Java are AND (&&), OR (||) and NOT (!).

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.

30. What is a null statement?


Ans. Statement which has only a semicolon is termed as null statement. These statements are generally
used where syntaxes of certain commands require a statement whereas the programmer may not want
to give it. In that case to abide by the syntax of the command the null statement is used.
31. What is a compound statement?
Ans. The group of statements within a block is referred to as a compound statement.
For example,
{
int a=5, b=6;
System.out.println(a+ “ “+b);
}
32. What do you understand by type conversion?
Ans. Converting explicitly using type-casting the resultant of a mathematical expression from
one data type to another is termed as type conversion.

33. State the importance of a "class".


Ans. The class acts as a skeleton for an object. The classes are the bases of an object. It is a template
upon which an object is built. The class is the mechanism that is used to create objects
34. Why is Java called a strongly typed language?
Ans. Java is a strongly-typed or statically-typed language in the sense that every variable has a type,
every expression has a type, and every type is strictly defined. Also all assignments, whether explicit or
via parameters passing in method calls, are checked for type compatibility.
35. How do you declare objects?
Ans. Objects are declared using the new command using the following syntax:
<Class-name> <object-name>= new <Class-name>();

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.

B. Answer the following programs:


1. Write a program to initialize two integer variables a and b with 5 and 6 respectively and
interchange them. Thus after interchanging a and b will be 6 and 5 respectively.
Ans. public class Question1

static void main()

int a=5,b=6,t;

System.out.println("Before Interchanging a="+a+" b="+b);

//Swapping the values

t=a;

a=b;

b=t;

System.out.println("After Interchanging a="+a+" b="+b);

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

C. Short-answer type questions:


1. Write the corresponding expressions for the following mathematical operations: -
i) a2 + b2
ii) z = x3 + y3 - xy /z
Ans. i) a*a + b*b
ii) z=x*3 + y*3 – x*y/z

2. 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);
Ans. a =99

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-+

4. What will be the output of the following, if x = 5 initially?


i) 5 *++x
ii) 5*x++
Ans. i) 30
ii) 25
5. 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
Ans. i) -22
ii) 8

6. If a = 5, b = 9 calculate the value of:


a+ = a++ - ++b + a
Ans. a=6 and b=10

7. What is the result stored in x, after evaluating the following expression


int x = 5;
x = x++ *2 + 3 * –x;
Ans. -8
8. Assign the value of pie (i.e. 3.142) to a variable with requisite data type.
Ans. double pi=3.142;

9. Which of the following are valid comments?


i) / * comment * 1
ii) / * comment
iii) // comment
iv) */ comment */
Ans. iii) // comment

10. Name the primitive data type in Java that is:


i) a 64-bit integer and is used when you need a range of values wider than those provided by it.
ii) a single 16 bit Unicode character whose default value is '100000'.
Ans. i) long
ii) char

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

E. State the result of c while using the following bitwise operators:


1. c=a & b when a=13 and b=4
2. c= a | b when a=22 and b=11
3. c=a ^ b when a=14 and b=32
4. c= ~a when a=36
5. c=a & b | c when a=12, b=13 and c=11
6. c=a | b & c ^ a when a=15, b=12 and c=10
7. c=a & (~b) when a=15 and b=12
8. c=(a | ~b) & ~c when a=15, b=12 and c=10
9. c=c ^ (a & ~b) when a=15, b=12 and c=10
10. c= a & b & c | a | b when a=15, b=12 and c=10
Ans. 1. c=4
2. c=31
3. c=46
4. c=-37
5. c=15
6. c=15
7. c=3
8. c=-11
9. c=9
10. c=15

F. State the result of c while using the following Shift Operators:


1. c=a >> 3 when a=–45
2. c=a<<2 when a=–33
3. c=a>>4 when a=97
4. c=a>>>26 when a=–97
5. c=a<<2 when a=12
Ans. 1. -6
2. -132
3. 6
4. 63
5. 48
G. 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);
}
Ans.
6
4

You might also like