0% found this document useful (0 votes)
189 views10 pages

Understanding Java Data Types and Values

This document contains a chapter on values and data types in Java with multiple choice questions, fill in the blanks questions, and short answer questions. The chapter covers topics like data types, variables, literals, constants, tokens, and casting in Java.

Uploaded by

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

Understanding Java Data Types and Values

This document contains a chapter on values and data types in Java with multiple choice questions, fill in the blanks questions, and short answer questions. The chapter covers topics like data types, variables, literals, constants, tokens, and casting in Java.

Uploaded by

RK Consultancy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Multiple Choice Questions
  • Write Short Answers
  • Fill in the Blanks

Learn java with blue j

Chapter 3

Values and Data Types


Multiple Choice Questions

Question 1: A constant which gives the exact representation of data is called

1. Variable
2. Literal ✓
3. Identifier
4. Character

Question 2: A word used in a high level language which has a special meaning attached to it is called

1. Class
2. Identifier
3. Keyword ✓
4. Literal

Question 3: A character literal is assigned to a:

1. Char variable ✓
2. Char type literal
3. String variable
4. String literal

Question 4: A character literal is enclosed in:

1. ''✓
2. ""
3. ::
4. {}

Question 5: A set of characters is assigned to:

1. String variable ✓
2. Static variable
3. Boolean variable
4. None

Rajanikant Mishra| Project manager


Learn java with blue j
Question 6: The ASCII codes of upper case alphabets range from:

1. 65 - 90 ✓
2. 60 - 85
3. 65 - 91
4. 97 - 122

Question 7: Which of the following results in integer type?

1. 11.4F/3.2D
2. 13.8F/4.6F;
3. 12/3 ✓
4. None

Question 8: Which of the following is non-primitive data?

1. char
2. long
3. object ✓
4. short

Question 9: Which of the following type is an exact representation of fractional values?

1. char
2. double ✓
3. byte
4. String

Question 10: Boolean Data is used to test a particular condition i.e. true or false. Which of the following is a
correct representation?

1. boolean m=true ✓
2. boolean m='true'
3. boolean m="true"
4. none

Rajanikant Mishra| Project manager


Learn java with blue j

Fill in the blanks

Question 1: The character sets of Java is like alphabets of English language.

Question 2: A standard encoding system way of representing characters is Unicode.

Question 3: ASCII code is decimal number to represent a character.

Question 4: Each individual component of a Java statement is known as token.

Question 5: In Java, the constants are also called literals.

Question 6: Assignment operator is used to store a value in the variable.

Question 7: The comma, exclamation, question mark etc., are termed as Tokens in Java language.

Question 8: An element of Java program that is used to identify a class, function or value is called
as identifier.

Question 9: Integer type value occupies 4 bytes in the memory.

Question 10: A Java expression that contains all the elements of same data type is pure expression.

Write short answers

Question 1

What do you mean by data type?

Data types are used to identify the type of data a memory location can hold and the associated
operations of handling it.

Question 2

Define variable with an example.

A variable represents a memory location through a symbolic name which holds a known or unknown
value of a particular data type. This name of the variable is used in the program to refer to the stored
value.
Example:
int rollNumber = 21; string myName= “UDAN –We are future”;

Question 3

Rajanikant Mishra| Project manager


Learn java with blue j
What do you mean by constant? Explain with an example.

The keyword final before a variable declaration makes it a constant. Its value never changed in the
program.
Example:
final int DAYS_IN_A_WEEK = 7;

Question 4

State two kinds of data types.

Two kinds of data types are:

1. Primitive Datatypes.
2. Non-Primitive Datatypes.

Question 5

What do you understand by Token? Name different types of tokens.

A token is the smallest element of a program that is meaningful to the compiler. The different types of
tokens in Java are:

1. Identifiers
2. Literals
3. Operators
4. Separators
5. Keywords

Question 6

What are the rules to assign a variable in a Java programming?

1. Name of the variable should be a sequence of alphabets, digits, underscore and dollar sign
characters only.
2. It should not start with a digit.
3. It should not be a keyword or a Boolean or null literal.

Question 7

Explain the term 'type casting'?

The process of converting one predefined type into another is called type casting.

Question 8

Perform the following:

Rajanikant Mishra| Project manager


Learn java with blue j
(a) Assign the value of pie (3.142) to a variable with the requisite data type.

double pi = 3.142;

(b) Assign the value of (1.732) to a variable with the requisite data type.

double x = 1.732;

Question 9

Distinguish between:

(a) Integer and floating constant

Integer Constant Floating Constant

Floating Constants represent fractional


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

Integer Constants are assigned to variables of Floating Constants are assigned to


data type — byte, short, int, long, char variables of data type — float, double

(b) Token and Identifier

Token Identifier

Identifiers are used to name things like


A token is the smallest element of a program
classes, objects, variables, arrays,
that is meaningful to the compiler.
functions an so on.

Tokens in Java are categorized into 5 types —


Keywords, Identifiers, Literals, Punctuators, Identifier is a type of token in Java.
Operators.

Rajanikant Mishra| Project manager


Learn java with blue j
(c) Character and String constant

Character Constant String Constant

String Constants are written by


Character Constants are written by enclosing a
enclosing a set of characters within a
character within a pair of single quotes.
pair of double quotes.

Character Constants are assigned to variables String Constants are assigned to


of type char. variables of type String.

(d) Character and Boolean literal

Character Literal Boolean Literal

A boolean literal can take only one of


Character literals are written by enclosing a
the two boolean values represented by
character within a pair of single quotes.
the words true or false.

Character literals can be assigned to variables


Boolean literals can only be assigned to
of any numeric data type — byte, short, int,
variables declared as boolean
long, float, double, char

Escape Sequences can be used to write Only true and false values are allowed
character literals for boolean literals

Question 10

Write down the data type of the following:

(a) Integer
int

(b) Long Integer


long

(c) A fractional number


double

(d) A special character


char

Rajanikant Mishra| Project manager


Learn java with blue j
Question 11

What do you understand by Boolean type data? Explain with an example.

A boolean data type is used to store one of the two boolean values — true or false. The size of boolean
data type is 8 bits or 1 byte.
Example:
boolean bTest = false;

Question 12

What do you understand by primitive data type? Give two examples.

Primitive data types are the basic or fundamental data types used to declare a variable. Examples of
primitive data types in Java are byte, short, int, long, float, double, char, boolean.

Question 13

Why is it necessary to define data type in Java programming?

Data types tells Java how much memory it should reserve for storing the value. Data types also help in
preventing errors as the compiler can check and flag illegal operations at compile time itself.

Question 14

Define the following with an example each:

(a) Implicit type conversion

In implicit type conversion, the result of a mixed mode expression is obtained in the higher most data
type of the variables without any intervention by the user. Example:

int a = 10;
float b = 25.5f, c;
c = a + b;
(b) Explicit type conversion

In explicit type conversion, the data gets converted to a type as specified by the programmer. For
example:

int a = 10;
double b = 25.5;
float c = (float)(a + b);

Question 15

Define 'Coercion' with reference to type conversion.

Rajanikant Mishra| Project manager


Learn java with blue j
In a mixed-mode expression, the process of promoting a data type into its higher most data type
available in the expression without any intervention by the user is known as Coercion.
Example:

byte b = 21; int i = 197716; float f = 13.5


double result = b + i + f;

Question 16

What do you mean by type conversion? How is implicit conversion different from explicit conversion?

The process of converting one predefined type into another is called type conversion. In an implicit
conversion, the result of a mixed mode expression is obtained in the higher most data type of the
variables without any intervention by the user. For example:

int a = 21;
float b = 19.5f, d;
d = a + b;
In case of explicit type conversion, the data gets converted to a type as specified by the programmer.
For example:

int a = 10;
double b = 25.5;
float c = (float)(a + b);

Question 17

In what way is static declaration different from dynamic declaration?

In static declaration, the initial value of the variable is provided as a literal at the time of declaration.
For example:

int mathScore = 100;


double p = 1.4142135;
char ch = 'A';
In dynamic declaration, the initial value of the variable is the result of an expression or the return
value of a method call. Dynamic declaration happens at runtime. For example:

int a = 4;
int b = [Link](a);

double x = 3.14159, y = 1.4142135;


double z = x + y;

Question 18

What do you mean by non-primitive data type? Give examples.

Rajanikant Mishra| Project manager


Learn java with blue j
A non-primitive data type is one that is derived from Primitive data types. A number of primitive data
types are used together to represent a non-primitive data type. Examples of non-primitive data types
in Java are Class and Array.

Question 19

Predict the return data type of the following:

(i)

int p; double q;
r = p+q;
[Link](r);

Answer

Return data type is double.

(ii)

float m;
p = m/3*([Link](4,3));
[Link](p);

Answer

Return data type is double.

Question 20

What are the resultant data types if the following implicit conversions are performed? Show the result with
flow lines.
int i; float f; double d; char c; byte b;

(a) i + c/b;

Answer

i + c/b;
⇒ int + char / byte
⇒ int + char
⇒ int

(b) f/d + c*f;

Rajanikant Mishra| Project manager


Learn java with blue j
Answer

f/d + c*f;
⇒ float / double + char * float
⇒ double + float
⇒ double

(c) i + f - b*c;

Answer

i + f - b*c;
⇒ int + float - byte * char
⇒ int + float - char
⇒ float - char
⇒ float

(d) (f/i)*c + b;

Answer

(f/i)*c + b;
⇒ (float / int) * char + byte
⇒ float * char + byte
⇒ float + byte
⇒ float

(e) i + f- c + b/d;

Answer

i + f- c + b/d;
⇒ int + float - char + byte / double
⇒ int + float - char + double
⇒ float - char + double
⇒ float + double
⇒ double

(f) i/c + f/b;

Answer

i/c + f/b
⇒ int / char + float / byte
⇒ int + float
⇒ float

Rajanikant Mishra| Project manager

Common questions

Powered by AI

Implicit type conversion, also known as coercion, is performed automatically by the Java compiler when it converts one data type to another without explicit instruction from the programmer. For instance, if you add an int to a float (int x = 2; float y = 3.0f; float result = x + y;), the int is automatically converted to a float, and the result is stored as a float . Explicit type conversion, also known as casting, requires the programmer to instruct the compiler on what type conversion to perform. For example, when adding an int and a double, and you desire the result to be a float, you would explicitly cast it: int a = 10; double b = 25.5; float result = (float) (a + b).

Character literals in Java are tied to their ASCII values, as each character is internally represented by a unique ASCII code. For instance, the character 'A' corresponds to the ASCII value 65. This correspondence facilitates various string manipulations, such as comparison operations or transformations, like converting a lowercase character to uppercase by modifying its ASCII value (e.g., 'char ch = 'a'; char upperCh = (char) (ch - 32);' results in 'A', considering ASCII values). Understanding ASCII values enables developers to create algorithms for encoding, decoding, or changing case, ensuring consistent and predictable string operations based on these fundamental character codes .

Defining data types in Java is crucial for several reasons. Firstly, data types help the compiler to understand how much memory should be allocated to store a value. For instance, an 'int' type is allocated 4 bytes, whereas a 'double' receives 8 bytes of memory . This precise allocation is essential for ensuring efficient memory usage and avoiding memory overflow errors. Secondly, data types facilitate error prevention by the compiler, which checks for type mismatches at compile time. This early detection of errors ensures that operations are only performed between compatible types, minimizing runtime errors and enhancing code robustness .

Non-primitive data types in Java, also known as reference types, are distinct from primitive data types due to their complexity and derivation from the latter. Primitive data types like int, char, and boolean are basic building blocks with direct memory allocations: each occupies a fixed amount of memory. Non-primitive types, such as Strings, Arrays, and Classes, are not directly derived from basic types; instead, they aggregate multiple primitive data types or are instantiated from them . For example, an Array can store multiple instances of any data type (e.g., int[] numbers = {1, 2, 3};), and a String, an object, represents sequences of characters without a fixed length, differing from a single char. Their complexity allows them to store more data, support methods, and enable complex operations that primitives cannot .

Coercion in Java facilitates operations in mixed-mode expressions by automatically promoting the data types involved to the highest precision among them, ensuring the operation proceeds without data loss or errors. For example, if a byte (8 bits), an int (32 bits), and a double (64 bits) participate in an expression (e.g., byte b = 21; int i = 197716; double d = 13.5; double result = b + i + d;), the smaller data types are implicitly converted to double, the highest precision type, to ensure accuracy and prevent overflow . This automatic type promotion simplifies coding by handling conversions that would otherwise require manual intervention, although developers need to be cautious about potential precision loss or performance costs incurred by unnecessary promotions in critical computations .

Literals in Java provide a way to represent fixed values directly in the source code, such as numerical values, characters, or strings. Unlike variables, literals do not have an identifier or a mutable value; they represent constant values written directly in the code. For instance, '3.14' in 'double pi = 3.14;' is a literal. Variables are names that represent storage locations in memory which can change state, while identifiers are names given to variables, functions, or other entities, helping the compiler recognize them. Thus, literals are immutable values directly used in code, whereas variables and identifiers serve as references or names for data residing in memory .

Boolean data types in Java are fundamental in control structures due to their binary nature, storing only two possible values: true or false. They are crucial in decision-making structures like if-else statements, loops, and conditional expressions, determining the flow of control based on conditional evaluations. For instance, within an if-else statement (if(condition) { ... } else { ... }), the condition must evaluate to a boolean. Similarly, boolean expressions control loop executions (while(condition) { ... }) or decide case selections in switch constructs. This binary evaluation capability makes booleans essential for implementing logic and decision-making in Java programs .

The types of tokens in Java include Keywords, Identifiers, Literals, Operators, and Separators. Keywords are reserved words for specific functions, like 'class' or 'public', indicating the purpose of a structure in the code. Identifiers are names assigned to variables or functions, helping in identification and reuse of program entities. Literals represent constant values such as numbers or strings that are directly written in the code. Operators are symbols that perform operations on variables and values, such as '+' for addition. Separators like commas, parentheses, and semicolons help in structuring the code, indicating ends of statements or separating different elements in expressions. These tokens are fundamental units that ensure the coherent structure and function of a Java program .

Type casting in Java plays a pivotal role in managing precision and ensuring compatibility between different data types. It facilitates explicit conversion of data from one type to another, enabling operations between incompatible types. For instance, performing a division involving an integer and a float necessitates casting to maintain precision and prevent truncation, e.g., 'int a = 10; float b = 2.5f; float c = (float) (a / b);' ensures the outcome reflects precise division rather than integer division. Type casting is crucial when exact data representation and operations are required, enabling programmers to control how conversions occur, thereby avoiding implicit conversions that may lead to precision loss or logic errors .

Static declaration involves initializing a variable at the time of declaration using a literal, such as 'int number = 10;', where the value is fixed and known at compile time. This can enhance performance as initialization is predictable and optimized by the compiler. However, it can reduce flexibility as it may lead to hard-coded values . Dynamic declaration, on the other hand, initializes a variable based on runtime results, such as the output of a function or an expression, offering flexibility. For example, 'int sqrtValue = Math.sqrt(input);' allows adaptation to varying inputs. Nevertheless, it might affect performance due to runtime computations and potential overhead . Balancing between static and dynamic approaches depends on the application needs concerning flexibility and performance.

Chapter 3
Values and Data Types
Multiple Choice Questions
Question 1: A constant which gives the exact representation of data
Question 6: The ASCII codes of upper case alphabets range from:
1. 65 - 90 ✓
2. 60 - 85
3. 65 - 91
4. 97 - 122
Question 7: Wh
Fill in the blanks
Question 1: The character sets of Java is like alphabets of English language.
Question 2: A standard encod
What do you mean by constant? Explain with an example.
The keyword final before a variable declaration makes it a constant. I
(a) Assign the value of pie (3.142) to a variable with the requisite data type.
double pi = 3.142;
(b) Assign the value of (1
(c) Character and String constant
Character Constant
String Constant
Character Constants are written by enclosing a 
characte
Question 11
What do you understand by Boolean type data? Explain with an example.
A boolean data type is used to store one of
In a mixed-mode expression, the process of promoting a data type into its higher most data type 
available in the expression
A non-primitive data type is one that is derived from Primitive data types. A number of primitive data 
types are used togeth
Answer
     f/d + c*f;
⇒ float / double + char * float
⇒ double + float
⇒ double
(c) i + f - b*c;
Answer
     i + f - b*c;
⇒

You might also like