[go: up one dir, main page]

0% found this document useful (0 votes)
5 views5 pages

Chapter 4 Book back notes

The document provides an overview of Java programming concepts, including tokens, keywords, identifiers, literals, and constants. It explains the rules for forming identifiers, the types of literals, and includes examples of valid and invalid identifiers. Additionally, it contains sample Java programs demonstrating calculations related to geometry and distance.

Uploaded by

darlinmary82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Chapter 4 Book back notes

The document provides an overview of Java programming concepts, including tokens, keywords, identifiers, literals, and constants. It explains the rules for forming identifiers, the types of literals, and includes examples of valid and invalid identifiers. Additionally, it contains sample Java programs demonstrating calculations related to geometry and distance.

Uploaded by

darlinmary82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Excel Central School, Thiruvattar.

Name: __________________ Grade – IX- B


Computer Applications
1. What is meant by token? Name the tokens available in Java.
A token is the smallest individual unit in a Java program that is meaningful to the
compiler.
The tokens available in Java are:
• Keywords: Reserved words that have a special meaning in the language (e.g., class,
public, static).
• Identifiers: Names given to variables, classes, methods, etc. (e.g., abc, $a123,_sad).
• Literals: Fixed values assigned to variables (e.g., 123, 'A', "Hello").
• Operators: Symbols that perform operations on variables and values (e.g., +, -, , /).
• Separators: Characters that separate tokens (e.g., ;, ,, ()).Comments: Non-executable
text for documentation (//, / */).
2. What are keywords? Can keywords be used as identifiers?
Keywords are reserved words in Java that have a predefined meaning and cannot be used
for any other purpose, such as defining variables, classes, or methods.
Examples include int, class, if, else, for, while, etc.
Keywords cannot be used as identifiers because they are reserved for specific syntactic
purposes in the language.
3. What is an identifier? What is the identifier forming rule of Java?
An identifier is a name given to elements such as variables, methods, classes, and objects
in a Java program.Identifier
Forming Rules:
• Must begin with a letter (A-Z or a-z), currency character ($), or an underscore ().
• Subsequent characters can be letters, digits (0-9), currency characters, or
underscores.
• Cannot use reserved keywords.
• Java identifiers are case-sensitive.
4. An identifier can start with the underscore "". True or False.
True. An identifier in Java can start with an underscore.
5. Is Java case sensitive? What is meant by the term 'case sensitive'?
Yes, Java is case sensitive. Case sensitive means that Java distinguishes between uppercase
and lowercase letters. For example, Variable and variable would be considered two different
identifiers.
6. An identifier in Java can begin with a digit or letter. True or False.
False. An identifier in Java can begin with a letter (A-Z or a-z), a currency character ($),
or an underscore (), but not with a digit.
7. All special characters are allowed in Java identifiers. True or False.
False. Only letters, digits, currency characters ($), and underscores (_) are allowed in Java
identifiers.
Special characters like @, #, !, etc., are not allowed.
8. Which of the following are valid identifiers and why/why not:
• Data_rec, _data, 1data, data 1, my.file, asm, switch, goto, break?
• Data_rec: Valid. It starts with a letter and contains only letters and an underscore.
• _data: Valid. It starts with an underscore and contains only letters.
• 1data: Invalid. It starts with a digit.
• data 1: Invalid. It contains a space.
• my.file: Invalid. It contains a period(DOT).
• asm: Valid. It is not a reserved keyword.
• switch: Invalid. It is a reserved keyword.
• goto: Invalid. It is a reserved keyword.
• break: Invalid. It is a reserved keyword.
9. What are literals? How many types of integer literals are available in Java?
Literals are fixed values assigned to variables. They represent constant values in the code.
There are four types of integer literals in Java:
• Decimal (base 10): e.g., 123
• Binary (base 2): e.g., 0b1010 [0,1]
• Octal (base 8): e.g., 0123 [0-7]
• Hexadecimal (base 16): e.g., 0x7B10. [0-9 and A-F]
10. What is an integer constant? Write integer forming rule of Java.
An integer constant is a literal that represents an integer value.
Integer Forming Rules:
• Must not have any fractional part. (ie., 23.56)
• Can be written in decimal (base 10),
• binary (base 2, prefixed with 0b),
• octal (base 8, prefixed with 0),
• hexadecimal (base 16, prefixed with 0x).
11. How many types of integer constants are allowed in Java? How are they written?
There are four types of integer constants in Java:
• Decimal: e.g., 123
• Binary: e.g., 0b1010
• Octal: e.g., 0123
• Hexadecimal: e.g., 0x7B
12. What kind of program elements are the following: 13, 'd', 4.38925, "d", main ( )?
• 13: Integer literal
• 'd': Character literal
• 4.38925: Floating-point literal
• "d": String literal
• main ( ): Method name with parentheses indicating a method call
13. What kind of constants are the following: 14, 011, 0X2A, 17, 014, 0XBC1?
• 14: Decimal integer constant
• 011: Octal integer constant (decimal value 9)
• 0X2A: Hexadecimal integer constant (decimal value 42)
• 17: Decimal integer constant
• 014: Octal integer constant (decimal value 12)
• 0XBC1: Hexadecimal integer constant (decimal value 3009)
14. What is a character constant in Java? How are non-graphic characters represented in
Java?
A character constant in Java is a single character enclosed in single quotes, such as 'A'.
Non-graphic characters, which are characters that cannot be displayed graphically (like
newline, tab), are represented using escape sequences.
Examples include:Newline: '\n'Tab: '\t'Backspace: '\b'Carriage return: '\r'Form feed: '\f'
15. Why are characters 'v', '\v', and typed using escape sequences?
The character 'v' is a printable character and can be directly typed. However, '\v' is an
escape sequence for a vertical tab, which is a non-printable character and must be
represented using an escape sequence.
16. Which escape sequences represent the newline character and null character?
Newline character: '\n'
Null character: '\0'
17. What is meant by a floating constant in Java? How many ways can a floating constant be
represented into?
A floating constant in Java is a numeric literal that represents a floating-point number.
Floating constants can be represented in two ways:
Decimal notation: e.g., 3.14, 0.001
18. Write the following real constants into exponent form:
23.197, 7.214, 0.00005, 0.319.
23.197
Move the decimal point one place to the left: 2.3197×10 1
2.3197 *101
7.214
The number is already between 1 and 10:
7.214×100
0.00005
Move the decimal point five places to the right:
5×10−5
0.319
Move the decimal point one place to the right:
3.19×10−1
19. Write the following real constants into fractional form:
0.13E04, 0.417E-04, 0.4E-05, 0.123E02
0.13E04: 1300
This means 0.13×104
1300
0.417E-04: 0.0000417
This means 0.417×10−4
Ie., 417/10000000
0.4E-05: 0.000004
This means 0.4×10−5
0.4 *10-5 =0.000004
Ie., 4/1000000
0.123E02: 12.3
This means 0.123×102
0.123 * 102 =12.3
In fractional form: 123/10.
20. Write a program to determine the length of the wooden strip required to frame a
photograph of length and breadth 32 cm and 21 cm respectively.
public class WoodenStrip {
public static void main(String[] args) {
int length = 32;
int breadth = 21;
int perimeter = 2 * (length + breadth);
System.out.println("The length of the wooden strip required: " + perimeter + " cm");
}
}
21. A rectangular piece of land measures 0.7 km by 0.5 km. Each side is to be fenced with 4
rows of wires. Write a program to determine the length of the wire needed.
public class Fencing {
public static void main(String[] args) {
double length = 0.7; // in km
double breadth = 0.5; // in km
int rows = 4;
double perimeter = 2 * (length + breadth); // in km
double totalWireLength = rows * perimeter; // in km
System.out.println("The length of the wire needed: " + totalWireLength + " km");
}
22. A student's school is at a distance of 5 km 350 m from her house. She travels 1 km 70 m
on foot and the rest by bus. Write a program to determine how much distance she travels
by bus. (Hint: Use constant, 1 km = 1000 meters)
public class TravelDistance {
public static void main(String[] args) {
final int METER_IN_KM = 1000;
int totalDistance = 5 * METER_IN_KM + 350; // in meters
int footDistance = 1 * METER_IN_KM + 70; // in meters
int busDistance = totalDistance - footDistance; // in meters
System.out.println("The distance traveled by bus: " + busDistance + " meters");
}
}
DATATYPES (SIZE IN BITS, BYTES AND RANGES)

You might also like