Data Types in Java
Class 8 - APC Understanding Computer Studies with BlueJ
Multiple Choice Questions (Tick the correct answers)
Question 1
A constant which gives the exact representation of data is called a/an:
1. variable
2. literal
3. identifier
4. character
Question 2
The default value of the boolean data type is:
1. "false"
2. true
3. false
4. "true"
Question 3
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. ' ' ⇐ Answer
2. " "
3. : :
4. { }
Question 5
A set of characters is assigned to a:
1. String variable
2. Static variable
3. Boolean variable
4. None
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 is false to find square of a number?
1. Math.pow(a,2)
2. a*a
3. Math.sqrt(a,2)
4. All
Question 8
What type of value is returned by the sqrt function?
1. int
2. float
3. double
4. All
Question 9
Which of the following syntax is true to find the square root of a number?
1. sqrt(a)
2. Math.sqrt(a)
3. Squareroot(a)
4. None
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
Short Answer Questions
Question 1
Write down the syntax for the following functions in Java programming:
(a) To find the smaller between two numbers.
Answer
Math.min(a, b)
(b) To find the absolute value of a number.
Answer
Math.abs(a)
(c) To find the exponent of a number.
Answer
Math.exp(a)
(d) To find the square root of a number.
Answer
Math.sqrt(a)
Question 2
Assign the value of pie () to a variable with the suitable data type.
Answer
double pi = 3.14159
Question 3
Give the output of the following expressions:
(a) If x = -9.99, calculate Math.abs(x);
Answer
9.99
(b) If x = 9.0, calculate Math.sqrt(x);
Answer
3.0
Question 4
Perform the following functions with a suitable data type to:
(a) assign 35.0012 to a variable.
Answer
double a = 35.0012;
(b) assign the value of √2 = (1.414) to a variable.
Answer
double a = Math.sqrt(2);
(c) assign 435.0 to a variable.
Answer
double a = 435.0;
(d) find the square root of 99 with a variable.
Answer
int a = 99;
double res = Math.sqrt(a);
(e) find the natural log of 23 with a suitable variable.
Answer
int a = 23;
double res = Math.log(a);
Question 5
Correct the given snippet:
int a = 45.1;
float b = 15.05;
double c = 92;
sum = 0;
sum = (a+b)*c/2;
System.out.println("The answer ="+ans);
Answer
Errors in the snippet
1. a is an int variable so a floating point literal can't be assigned to it. a
should be of double type.
2. b is float variable but 15.05 is double literal. We need to convert 15.05
to float literal by appending f to it.
3. Variable sum is not declared.
4. In the last line, System.out.println("The answer ="+ans); we should
print sum not ans.
Corrected Program
double a = 45.1; //1st correction
float b = 15.05f; //2nd correction
double c = 92;
double sum = 0; //3rd correction
sum = (a + b) * c / 2;
System.out.println("The answer =" + sum); //4th correction
Question 6
Write down the data type of the following:
(a) Integer ⇒ int
(b) Long Integer ⇒ long
(c) A fractional number ⇒ double
Long Answer Questions
Question 1
Define 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;
Question 2
What is meant by a constant? Explain with an example.
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;
Question 3
What are the rules to assign a variable in Java programming?
Answer
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 4
Define the following:
(a) Compiler
Answer
A compiler is a program that translates a source program written in
some high-level programming language into a target program in
another low-level programming language without changing the
meaning of the program. The compiler processes the complete source
program at once and if there are compilation errors, they are all
reported together at once.
(b) Interpreter
Answer
An interpreter is a program that reads a source program line by line,
converts each line into its equivalent machine code and executes it. As
it reads the program line by line so the errors are reported one by one.
(c) Byte code
Answer
Java compiler converts Java source code into an intermediate binary
code called Bytecode. Bytecode can't be executed directly on the
processor. It needs to be converted into Machine Code first.
Question 5
Distinguish between the following:
(a) Integer and Floating literal
Answer
Integer Literal Floating Literal
Integer literal represents whole Floating literal represents
number values like 2, -16, fractional numbers like 3.14159,
18246, 24041973, etc. -14.08, 42.0, 675.238, etc.
Integer Literal Floating Literal
Integer literals are assigned to Floating literals are assigned to
variables of data type — byte, variables of data type — float,
short, int, long, char double
(b) Character and String literal
Answer
Character literal String literal
Character literals are written by String literals are written by
enclosing a character within a enclosing a set of characters
pair of single quotes. within a pair of double quotes.
Character literals are assigned String literals are assigned to
to variables of type char. variables of type String.
(c) Source code and Object code
Answer
Source Code Object Code
It is a set of statements in a High-Level It is a set of statements in
programming language. Machine Language.
It is understood by the
It is understood by human/programmer.
processor.
(d) Compiler and Interpreter
Answer
Compiler Interpreter
It translates the whole source It translates the source program
program into target program at into target program one line at a
once. time.
All the errors found during Errors are displayed line by line
compilation are displayed as each line is translated and
together at once. executed.
Question 6
What is meant by primitive and non-primitive data types? Give an example
of each.
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.
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 7
Who developed Java? What was it initially called?
Answer
James Gosling developed Java and it was called Oak initially.
Question 8
Mention at least four features of Java.
Answer
Four features of Java are:
1. It is an Object Oriented Programming Language.
2. It is platform independent. It provides us Write Once, Run
Anywhere (WORA) feature.
3. It uses a compiler as well as an interpreter.
4. It is case sensitive.
Question 9
What is a package? Give an example.
Answer
In Java, a package is used to group related classes. Packages are of 2
types:
1. Built-In packages — These are provided by Java API
2. User-Defined packages — These are created by the programmers
to efficiently structure their code.
java.util, java.lang are a couple of examples of built-in packages.
Question 10
What is meant by implicit and explicit type conversion? Explain with an
example.
Answer
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;
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 11
Java program uses a compiler as well as an interpreter. Explain.
Answer
Java compiler compiles Java source code to Bytecode. Bytecode
cannot run on the processor directly as processor only understands
Machine Code. Java Virtual Machine (JVM) takes this Bytecode as
input and converts it into Machine Code line by line. So, JVM acts as
an interpreter for converting Bytecode to Machine Code. In this way, a
Java program uses both a Compiler as well as an Interpreter to get
executed on the processor.
Question 12
Java is case sensitive. Explain.
Answer
Java is case sensitive means that it distinguishes between upper case
and lower case characters. Consider the below code snippet:
int studentMarks;
StudentMarks = 85;
This will give a compilation error as Java will
treat studentMarks and StudentMarks as two different variables because
the case of the characters is not same in both.