0% found this document useful (0 votes)
2 views

Java Data types

Chapter 3 covers values and data types in Java, including the use of Unicode, escape sequences, and the concept of tokens, keywords, and identifiers. It explains variable initialization, primitive data types, and the importance of comments in code documentation. Additionally, it provides examples of valid and invalid identifiers, as well as the output of specific Java code snippets.

Uploaded by

Saradha S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Data types

Chapter 3 covers values and data types in Java, including the use of Unicode, escape sequences, and the concept of tokens, keywords, and identifiers. It explains variable initialization, primitive data types, and the importance of comments in code documentation. Additionally, it provides examples of valid and invalid identifiers, as well as the output of specific Java code snippets.

Uploaded by

Saradha S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CHAPTER 3

Values and Data Types


Section 3: Assignment Questions

1. Why does Java use the Unicode character set?


Ans. Unicode is an international character set designed to represent all the characters found in
languages around the world, such as English, Hindi, Japanese, Chinese, French, and German. It can use 8
to 32 bits to represent a character. The first 128 characters in the Unicode character set are the same as
those of the ASCII character set, and the next set of 128 characters is the same as the Extended ASCII
character set. The rest are used to represent characters from all over the world making it the largest set
of characters. Java uses the Unicode character encoding thus enables us to handle text in any language.

2. What are escape sequences in Java? Give three examples.


Ans. An Escape Sequence is a set of characters that has a special meaning to the Java compiler. In the
escape sequence, a character is preceded by a backslash (\). An escape sequence causes the character
following it to escape its normal interpretation. For example: when you print the character 'n', the same
character is displayed on the screen, but when it is prefixed with '\', it is read as an escape sequence. \n
Inserts a newline in the text at this point (shifts control to the next line) \t Inserts a tab in the text at this
point \\ Inserts a backslash character in the text at this point

3. What is the result of evaluating the following expression?


(3 + 6 * 7) / 3 + 2
Ans. Output: 17

4. What is a token in Java? Name the tokens available in Java.


Ans. All characters in a Java program are grouped into symbols called Tokens. As we know, a computer
program consists of a set of instructions called statements. The statement is composed of various
components. Each individual component of the programming statement is referred to as a token.
Keywords, identifiers, and literals are the three tokens in Java.

5. Why can a keyword not be used as a variable name?


Ans. 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. These words have a special meaning to
the Java compiler. For example void and public.

6. Which of the following are Java keywords?


area, input, class, public, int, x, y, radius, long, Hello.java
Ans. input, class, public, int, long.

7. What are identifiers in Java? List three identifier formation rules.


Ans. Identifiers are used to name different parts of a program such as variables, methods, classes,
objects, etc. An identifier can consist of any combination of letters, digits, the underscore character and
the dollar sign. An identifier cannot begin with a digit. Identifiers may be of any length. Both uppercase
and lowercase letters can be used in naming an identifier.
8. Explain the following statement – "In Java, total, Total, ToTaL, and TOTAL are all different
identifiers."
Ans. Java is case sensitive, which means that two identifier names that differ only in upper and lower
case characters are considered to be different identifiers. Therefore, total, Total, ToTaL, and TOTAL are
all different identifiers

9. Which of the following are invalid identifiers?

i. ten Ans. valid v. $dollar Ans. valid


ii. "Hello" Ans. invalid vi. 4Variables Ans. invalid
iii. 5678 Ans. invalid vii. _var Ans. valid
iv. Coffee Ans. valid

10. How would you print characters like \, ' and " in Java?
Ans. We can print characters like \, ‘and “ in Java with the help of escape sequence.

11. Distinguish between the following:

i. Token and Identifier


Ans. All characters in a Java program are grouped into symbols called Tokens. A computer program
consists of a set of instructions called statements. A statement is composed of various components.
Each individual component of a programming statement is referred to as a token. Keywords, identifiers,
and literals are tokens in Java.
Identifiers are used to name different parts of a program such as variables, methods, classes, objects
etc.

ii. Keyword and Identifier


Ans. Keyboards 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. These words have a special meaning to
the Java compiler. For example: void and public.
Identifiers are used to name different parts of a program such as variables, methods, classes, objects,
etc. An identifier can consist of any combination of letters, digits, the underscore character, and the
dollar sign. An identifier cannot begin with a digit. It may be of any length and both uppercase and
lowercase letters can be used in naming an identifier.

iii. Character and String Constant


Ans. A character constant is always enclosed within single quotes. For example: ‘a’, ‘A’.
A string constant is always enclosed within double quotes. For example: “a”, “A”, “Hello”
iv. Integer and Float Constant
www.bhuvantechs.com

Ans. Integer constants are values with whole numbers and floating constants can have decimal values.
For example: integer constants are -128, 127 and floating constants are – 1.5f, 1.5F.

12. Distinguish between "A" and 'A'.


Ans. “A” is a string constant and ‘A’ is a character constant.

13. What is wrong with the following statement?


float flt = 7895.0345;
Ans. Incompatible type conversion from double to float

Values and Data Types ~2~


14. Describe primitive data types in Java.
Ans. Primitive data types are fundamental data types that are an integral part of the Java language. The
names of three primitive data types are int, float and boolean.

15. List the size of primitive data types in Java.


Ans. Type Size
byte 8 bits
short 16 bits
int, float 32 bits
long, double 64 bits
char 16 bits
boolean 8 bits

16. Which integer and floating point data types take up the same number of bits in computer memory?
Ans. (i) int and float (ii) double and long

17. What is variable initialization in Java? What are the default values of the following type of
variables - short, int, long, float, double, and char?
Ans. Variable initialization is assigning value to a variable during declaration.
Type Default value
short 0
int 0
long 0L
float 0.0f
double 0.0d
char ‘\u0000’

18. Provide the declaration for two variables called xCoordinate and yCoordinate. Both variables are
of type int and both are to be initialised to zero in the declaration.
Ans. int xCoordinate = 0, yCoordinate = 0 ;

19. Write a Java assignment statement that will set the value of the variable interestAmount to the
value of the variable balanceAmount multiplied by the value of the variable rate. The variables are of
type double.
Ans. double interestAmount, rate, balanceAmount;
interestAmount = balanceAmount * rate;

20. Explain the statement, "a well-documented code is as important as the correctly working code".
Ans. Comments are important part of program and are actually added to give an overview of the code
www.bhuvantechs.com

and provide additional information that is not readily available in it. The primary purpose of comment is
to document the code so that even a layperson can understand the purpose of the written code.
Comments are ignored by the compiler and do not affect the program execution.

21. How can you write single line comments in Java?


Ans. This type of comment begins with a double slash (//) and continues to the end of line.

22. Write a Java constant declaration that gives the name TAX_RATE to the value 15%.
Ans. TAX_RATE=.15;

Values and Data Types ~3~


23. If you want to change the precedence of operations in an expression, which symbols do you use?
Ans. We can change the precedence of operations in an expression by using parenthesis ( ).

24. What are symbolic constants? How are they useful in writing programs?
Ans. Memory locations whose values cannot be changed within a program are called constants or
symbolic constants. When the program is compiled, each occurrence of a symbolic constant is replaced
by its corresponding character sequence.

25. What is the output produced by the following lines of program code?
char x, y;
x = 'y';
System.out.println(x);
y = 'z';
System.out.println(y);
x = y;
System.out.println(x);
Ans. Output:
y
z
z

www.bhuvantechs.com

Values and Data Types ~4~

You might also like