Chapter 1 - Unit 3
Values and Data Types
Class 10 - APC Understanding Computer Applications with BlueJ
State whether the following statements are 'True' or 'False'
Question 1
There are 128 set of different characters used in a Java program.
False
Explanation:
Java uses Unicode which can represent much more than 128 characters
Question 2
The ASCII codes of upper case letters range from 97 to 122.
False
Explanation: ASCII codes of upper case letters range from 65 to 90
Question 3
A variable gives the exact representation of data.
False
Explanation: A literal gives exact representation of data. As value of variable can change, so it cannot give exact representation of data.
Question 4
The data types int, float, char are called non-primitive types.
False
Explanation: The data types int, float, char are called Primitive types.
Question 5
A String literal is assigned to a String variable.
True
Explanation:
The data type of the variable that can hold a String literal should also be String.
Question 6
A character literal is always enclosed in double quotes.
False
Explanation: A character literal is enclosed in single quotes.
Question 7
String constant can be written by using a set of alphanumeric characters.
True
Explanation:
A String literal or String constant is defined by enclosing a set of alphanumeric characters within double quotes.
Question 8
An object is said to be a non-primitive data.
True
Explanation:
Class is a non-primitive data type so Objects are non-primitive data.
Question 9
The data type int stores fractional values.
False
Explanation: float or double data type stores fractional values.
Question 10
Boolean type data is used to test a condition and results in either true or false.
True
Explanation:
Boolean data type can be either true or false.
Write short answers
Question 1
What is meant by data type? Name two types of data type.
Answer
Data types are used to identify the type of data a memory location can hold and the associated operations of handling it. Data Types are of two types:
- Primitive Data Types
- Reference or Composite Data Types
Question 2
Why is it necessary to define data type in Java programming?
Answer
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 3
Define the following with an example:
(a) variable
Answer
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 mathScore = 95;
(b) constant
Answer
The keyword final before a variable declaration makes it a constant. Its value can't be changed in the program.
Example:final int DAYS_IN_A_WEEK = 7;
(c) boolean data type
Answer
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;
(d) coercion
Answer
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 = 42;
int i = 50000;
double result = b + i;
(e) primitive data type
Answer
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.
(f) non-primitive data type
Answer
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 4
What is a token? Name different types of tokens.
Answer
A token is the smallest element of a program that is meaningful to the compiler. The different types of tokens in Java are:
- Identifiers
- Literals
- Operators
- Separators
- Keywords
Question 5
Explain the term type casting.
Answer
The process of converting one predefined type into another is called type casting.
Question 6
Assign the following to a variable with suitable data type.
(a) m = 22 / 7
Answer
double m = (22.0 / 7.0);
(b) p= 1.4142135 (value of square root of 2)
Answer
double p = 1.4142135;
(c) k= 0.00004545
Answer
double k = 0.00004545;
(d) n=24.50
Answer
float n = 24.50f;
Question 7
Distinguish between:
(a) Token and Identifier
Answer
Token | Identifier |
---|---|
A token is the smallest element of a program that is meaningful to the compiler. | Identifiers are used to name things like classes, objects, variables, arrays, functions an so on. |
(b) Character and Boolean literal
Answer
Character literal | Boolean literal |
---|---|
Character literals are written by enclosing a character within a pair of single quotes. | A boolean literal can take only one of the two boolean values represented by the words true or false. |
Character literals can be assigned to variables of any numeric data type — byte, short, int, long, float, double, char | Boolean literals can only be assigned to variables declared as boolean |
Escape Sequences can be used to write character literals | Only true and false values are allowed for boolean literals |
Question 8
Explain the term type conversion. How is implicit conversion different from explicit conversion?
Answer
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 = 10;
float b = 25.5f, c;
c = 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 9
Classify the following as primitive or non-primitive data types.
Answer
(a) char
Primitive
(b) arrays
non-primitive
(c) int
Primitive
(d) classes
non-primitive
Question 10
In what way is static initialization of data type different from dynamic initialization?
Answer
In static initialization, 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 initialization, the initial value of the variable is the result of an expression or the return value of a method call. Dynamic initialization happens at runtime. For example:
int a = 4;
int b = Math.sqrt(a);
double x = 3.14159, y = 1.4142135;
double z = x + y;
Question 11
Predict the return data type of 'r' and 'n' from the snippet:
int p; float m;
r = p+m;
n = m/3*(Math.pow(4,3));
System.out.println(r);
System.out.println(n);
Answer
Return type of r is float and n is double.
Question 12
Give reason whether the following assignments are correct or no
(a) int m =155;
Answer
This assignment is correct as 155 is an integer literal and it is assigned to an int variable m.
(b) float f = 0.002654132;
Answer
This assignment is incorrect as data type of 0.002654132 is double but it is assigned to a float variable. The correct assignment will be float f = 0.002654132f;
(c) String str = 'Computer';
Answer
This assignment is incorrect as the String literal Computer is enclosed in single quotes. It should be in double quotes. The correct assignment will be String str = "Computer";
(d) boolean p = false;
Answer
This assignment is correct as false is a valid boolean literal and it is assigned to a boolean variable.
(e) String b = "true";
Answer
This assignment is correct as "true" is a string literal not a boolean literal as it is enclosed in double quotes. It is correctly assigned to a String variable.
(f) char ch = "apps";
Answer
This assignment is incorrect as "apps" is a string literal not a character literal and it is assigned to a variable ch of char data type.
(g) String st= "Application";
Answer
This assignment is correct as "Application" is a string literal and it is correctly assigned to a String variable.
(h) double n = 455.29044125;
Answer
This assignment is correct as 455.29044125 is a literal of double data type and it is correctly assigned to a double variable.
No comments:
Post a Comment
any problem in any program comment:-