Questions and Answers on Datatypes and Wrapper Class

Datatype and Wrapper class questions and answers will help you to clear concept and increase your knowledge on the previous two discussed chapters. These questions can be asked in any interviews, college vivas etc. I hope you will learn something out of these questions and answers.

  1. What is the difference between wrapper class and datatypes?

Firstly, Datatype makes Java partially object-oriented, whereas wrapper class makes java purely object-oriented.

Secondly, datatype allocated memory, whereas wrapper class manipulates datatypes.

2. What is Void?

The Void is a non-primitive wrapper class and can be used to create new objects which have methods that don't return any value.

3. What are the datatypes present in Java?

byte, boolean, int, char, float, double, long, short.

4. What are wrapper class and state its use in Java?

Java provides one class for each datatype which is called Wrapper class. It is used in the replacement of datatype in object format.

5. Which language does not support datatype and what would be the problem out of it?

All scripting languages do not support datatype. The problem is that we cannot implement a new data structure using them.

6. In which situation one should use Byte datatype?

To replace the integer type since by default number is an integer.

7. What is Nul in Java?

In the ASCII chart, the character corresponding to the 1st decimal value i.e. 0 (zero) is known as Nul.

Did you know?

Null is the default value of any reference or object and it is not a keyword.

8. How to calculate size and range of each data type?

For size use Datatype.size and for range use Datatype.MAX_VALUE for upper limit of range and Datatype.MIN_VALUE

for the lower limit of a range.

9. Is it possible to inherit a wrapper class in Java?

No, since the wrapper class is the final class and hence cannot be inherited.

10. How can one manipulate datatype in Java?

By using Wrapper class in place of the datatype, manipulation can be done.

11. What is the benefit of 'character' in Java?

As the 'character' in Java supports Unicode, we can use both regional character and ASCII characters in a program.

12. Write a program to add 2 numbers without using any datatype?

The same can be achieved if we use command line arguments.

public class Test {
public static void main(String[] args) {
Integer no1= Integer.parseInt(args[0]);
Integer no2= Integer.parseInt(args[1]);
System.out.println(no1 + no2);
}
}




13. Write a program to add two numbers using wrapper class?

public class Test {
	public static void main(String[] args) {
		Integer no1= 10; //Interger is a Wrapper Class
		Integer no2= 20;
		System.out.println(no1 + no2);
	}
}