Java Tokens and Data types Practice Questions
- Categories Java, Tokens & Data Types, Values & Data Types
Today we are going to practice short questions on Java Tokens and Data Types. Before you attempt the questions, do a quick review of these two topics by going through our video links here.
- Identify whether following are primitive or non-primitive types
(i) String (ii) arrays (iii) char (iv) classes
Ans.
(i) String – Non-Primitive
(ii) arrays – Non primitive
(iii) char – Primitive
(iv) Classes – Non primitive
- List the variables from those given below that are composite data types.
(i) static int x;
(ii) data[2] = 10;
Ans.
(i) x is of a primitive data type
(ii) data is variable of a composite or non primitive data type
- List the variables from those given below that are composite data types.
- private char chr;
- String str;
Ans.
- chr is a variable of a primitive data type
- str is a variable of a composite data type – String is a class type
- List the variables from those given below that are composite data types.
- display();
- boolean b;
- Scanner sc;
- Z = ‘\n’;
Ans.
- obj is a variable of a composite data type
- b is a variable of a primitive data type
- sc is a variable of a composite data type as Scanner is a class
- Z is of type char so non primitive type.
- Arrange the following primitive data types in an ascending order of their size:
(i) char (ii) byte
(iii) double (iv) int
Ans. byte, char, int, double
- (i) State the number of bytes occupied by char and int data types?
(ii) Write one difference between / and % operator.
Ans.
(i) char occupies two bytes and int occupied 4 bytes.
(ii) / is division operator while % is modulus operator which gives the remainder on dividing two numbers.
- Write the size of the following integer data types in terms of bytes.
- int
- float
- double
- char
Ans.
- int: 4 bytes
- float: 4 bytes
- double: 8 bytes
- char: 2 bytes
- What are the default values of the primitive data type int and float?
Ans. The default value of int is 0 and float is 0.0F
- Identify what is the type of literals listed below:
(i) 0.5 (ii)’A’ (iii) false (iv) “a”
Ans.
(i) Floating point literal
(ii) Character literal, single character in single quotes
(iii) Boolean literal
(iv) String literal, multiple characters in double quotes.
- Identify the literals as given below:
i. 246 ii. ‘c’ iii. true iv. “SimplyCoding”
Ans. i. int ii. char iii. boolean iv. String
- What is the range of the following data types:
- byte
- short
- long
- boolean
Ans:
- byte -> -128 to 127
- short -> -32768 to 32767
- long -> -263 to 263-1
- boolean – > true or false
- Identify the literals listed below :
- 2.5
- ’s’
- false
- ”Simply”
Ans :
- Real literal
- character literal
- boolean literal
- String literal
- Identify the following as valid variable names, state reason if invalid :
- 3
- D253
- Emp_name
- Salaryamount#
Ans.
- Invalid, dot (.) is not allowed.
- Valid
- Valid
- Invalid, # not allowed
- Identify the following as valid variable names, state reason if invalid :
- S%
- _A124
Ans.
- Invalid, special character (%) not allowed.
- Valid _ is allowed
- Identify the following as valid variable names, state reason if invalid :
- _250
- super
- ST 106
- While
Ans.
- valid as it begins with (underscore).
- Invalid, as super is a keyword.
- Invalid, space is not allowed.
- Valid, W is capital so it is not a key word.
- Identify the following literals as character/string literals:
- \t
- Simply coding#?9
- \26
- \ r
- \Simply
Ans.
- Character
- String
- String
- Character
- String
- Which of the following set of values can be assigned to a boolean variable?
- Yes and No
- true and false
- 0 and 1
- high and low
Ans. b. true and false (variable Boolean can be true or false)
- Write java statements for following:
- Declare double variable.
- Declare float variable and assign any value to it.
- Declare Boolean variable and assign value to it.
Ans.
- double x;
- float a = 3.14;
- boolean r = true;
- Predict the data type of the following:
i) int p; double q; ii) float m;
r = p + q; p = m/3*(Math.pow(4,3));
Systems.out.println(r); System.out.println(p);
Ans. i) double ii) double
- What is the resultant data type of the following mathematical expression?
a+b*c-d
- where a is int, b is int, c is float and d is float type
- where a, b, c and d are of int type, however the expression is slightly modified as
(a+b*c-d)/7.0
Ans.
- float
- double
- What is the resultant data type of the following mathematical expression?
a+b*c-d
- where a is of double and b,c and d are of int type
- where a is char and b,c and d are of int type
Ans.
- double
- int
- Identify errors in following statements and correct them:
- int number = 3,250;
- char ch = “Simply”;
Ans. correct statement:
- int number = 3250; // comma not allowed
- String ch = “Simply”; // char data type can have only 1 character
- Identify errors in following statements and correct them:
- int x/10 = y;
- int num = +25;
Ans. correct statement:
- int y= x/5;
- int num+ = 25;