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

Tokens and Values and Data Types

Uploaded by

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

Tokens and Values and Data Types

Uploaded by

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

ISA – TOKENS & VALUES and DATA TYPES

Question 1

What are escape sequences in Java? Give three examples.

Answer

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 (\). Some examples of escape
sequences are \n, \' and \t.

Question 2

What is the result of evaluating the following expression?


(1 + 2 * 2) / 2 + 2

Answer

(1 + 2 * 2) / 2 + 2 ⇒ (1 + 4) / 2 + 2
⇒ (5) / 2 + 2
⇒2+2
⇒4

Question 3

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

Answer

All characters in a Java program are grouped into symbols called Tokens. As you know, 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, Operators, Separators and literals are three tokens in
Java.

Question 4

Why can't you use a keyword as a variable name?

1|Page
ISA – TOKENS & VALUES and DATA TYPES

Answer

Keywords are reserved words that have a special meaning to the Java compiler. As Java
compiler reserves these words for its own use so they are not available as names for variables
or methods.

Question 5

Which of the following are Java keywords?


input, class, public, int, x, y, radius

Answer

class, public, int are Java keywords.

Question 6

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

Answer

Identifiers are used to name different parts of a program such as variables, methods, classes,
objects, etc. Three identifier formation rules are:

1. An an identifier can be a sequence of alphabets, digits, underscore and dollar sign


characters only.
2. Identifiers cannot start with a digit.
3. An identifier must not be a Keyword or a Boolean or null literal.

Question 7

Explain the following statement — "In Java, total, Total, ToTaL, and TOTAL are all
different identifiers."

Answer

2|Page
ISA – TOKENS & VALUES and DATA TYPES

Java is a case sensitive language. total, Total, ToTaL, and TOTAL have the same set of
letters but they differ in the case of these letters. Therefore, Java considers each of them as a
different identifier.

Question 8

How would you print characters like \, ' and " in Java?

Answer

We can print characters like \, ' and " using escape sequences i.e. preceding it with a
backslash (\) symbol.

Question 9

Distinguish between the following:

i. Token and Identifier

Answer

Token Identifier

Identifiers are fundamental building blocks of


Each individual component of a
the program and are used to name different
programming statement is
components of a program such as variables,
referred to as a token.
methods and objects.

Tokens in Java are categorised


into 5 types — Keywords,
Identifier is a type of token in Java.
Identifiers, Literals, Punctuators,
Operators.

ii. Keyword and Identifier

Answer

3|Page
ISA – TOKENS & VALUES and DATA TYPES

Keyword Identifier

Identifiers are used to name different components


Keywords have a special
of a program such as variables, methods and
meaning for Java compiler.
objects.

Keywords are reserved by


An identifier must not be a Keyword.
the compiler for its own use.

iii. Character and String Constant

Answer

Character Constant String Constant

Character Constants are written by String Constants are written by


enclosing a character within a pair of enclosing a set of characters within a
single quotes. pair of double quotes.

Character Constants are assigned to String Constants are assigned to


variables of type char. variables of type String.

iv. Integer and float Constant

Answer

Integer Constant Float Constant

Integer Constants represent whole Float Constants represent fractional


number values like 2, -16, 18246, numbers like 3.14159, -14.08, 42.0,
24041973, etc. 675.238, etc.

Integer Constants are assigned to


Float Constants are assigned to
variables of data type — byte, short, int,

4|Page
ISA – TOKENS & VALUES and DATA TYPES

Integer Constant Float Constant

long, char variables of data type — float, double

Question 10

Distinguish between "A" and 'A'.

Answer

"A" is a string literal of length 1 containing the letter A in uppercase whereas 'A' is a
character literal having value of A in uppercase.

Question 11

Describe primitive data types in Java.

Answer

Primitive data types are fundamental data types that are an integral part of the Java language
and are used to declare a variable.

Question 12

List the size of primitive data types in Java.

Answer

Data Type Size in Bytes

byte 1

short 2

int 4

5|Page
ISA – TOKENS & VALUES and DATA TYPES

Data Type Size in Bytes

long 8

float 4

double 8

char 2

boolean 1

Question 13

Which integer and floating point data types take up the same number of bits in
computer's memory?

Answer

Both, int and float take up 32 bits in memory. Similarly, both long and double take up 64 bits
in memory.

Question 14

What is variable initialisation in Java? What are the default values of the following type
of variables:
short, int, long, float, double, and char.

Answer

Variable initialisation means assigning value to a variable for the first time. Below are the
default values of the different data types:

Data Type Default Value

6|Page
ISA – TOKENS & VALUES and DATA TYPES

Data Type Default Value

short 0

int 0

long 0L

float 0.0f

double 0.0d

char '\u0000'

List of Java Reserved Keywords


Keyword Description

abstract Indicates the class or method that follows this keyword is abstract and
that will have to be implemented by a subclass.

assert Assert keyword helps the programmer to declare assertions or


assumptions in a program. If an assertion is true, program progresses
normally otherwise the AssertionError is thrown at runtime and program
aborts.

boolean Defines two Boolean values, true or false, 0 and 1.

break Used to break out of loops or iterative constructs.

byte Data type capable of holding 8-bit data.

case Marks blocks of text (cases) in a Switch statement.

7|Page
ISA – TOKENS & VALUES and DATA TYPES

catch Used to catch exceptions generated in the try block.

char Data type able to hold unsigned 16-bit Unicode characters.

class Used to declare a new class.

continue It helps to take control outside the loop and continue to the next iteration.

default Defines the "block of code" that will execute by default in a Switch
statement.

do Starting keyword for "do-while" loop.

double Data type holding 64-bit numbers (floating-point).

else Defines else part in the 'if' statements.

enum Used to declare enumerations in Java.

extends Indicates inheritance. A class is derived or inherited from another class.

final Defines a variable which will hold constant values or a method that
cannot be overridden.

finally Defines the finally block that executes after the try-catch block
irrespective of whether the exception was caught or not.

float Data type able to hold 32-bit floating-point values.

for Indicates the start of a 'for' loop.

if Start of 'if' statement.

implements Indicates that a class implements an interface.

8|Page
ISA – TOKENS & VALUES and DATA TYPES

import Used to include or reference other packages/classes into the program.

instanceOf Used to check if the given object is an instance of another class.

int Data type to hold a 32-bit integer value.

interface Used for declaring an interface.

long Data type holding 64-bit integer values.

native Used to indicate native code (platform-specific).

new Operator to create a new object.

null Indicates null reference.

package Keyword to declare Java package.

private Indicates private access specified which means a variable or method can
be accessed only by the class in which it is declared.

protected This keyword indicates a protected access specifier. When a variable or


method is protected then that variable or method can be accessed only by
the class they are declared in, its subclass, and other classes in the same
package.

public The public keyword is used to indicate public access specifier. A


variable, method, classes, interfaces declared as public can be accessed
throughput the application.

return Return is used to send back the value of a method to the calling method.
It also is used to return the control to the calling method.

short Data type holding 16-bit integer number values.

static The static keyword indicates the method or a variable is static and cannot

9|Page
ISA – TOKENS & VALUES and DATA TYPES

be instantiated.

strictfp The keyword strictfp restricts the rounding and precision of floating point
values calculation. It ensures portability.

super Indicates base or superclass of the class.

switch Indicates a Switch statement that tests a condition and executes multiple
cases depending on the test value.

synchronized Indicates synchronized sections for multithreaded code like critical


section.

this The keyword 'this' indicates the current object.

throw Throws an exception.

throws This indicates the exception that can be thrown by a method.

transient Specifies transient variable that is not part of the persistent state of an
object.

try Try keywords start a block that contains code that might raise exceptions.

void Indicates no return value.

volatile Used to define variables that are not stored in Main Memory. They can
be changed asynchronously.

while Keyword while starts a while loop.

const The 'const' keyword is no more supported in Java

goto The 'goto' keyword is no more supported in Java

true, false and The words "true, false, null" are literals. Still, we cannot use them as

10 | P a g e
ISA – TOKENS & VALUES and DATA TYPES

null identifiers in the program.

11 | P a g e

You might also like