Short Questions on Package/Wrapper classes
- Categories Java, Wrapper Classes
[A1]Add more questions here
Q. 1 Name of the following:
- A keyword used to include a package in the program.
- Any one reference data types.
Ans.
- import
- String
Q. 2 Write Java statement as directed:
- Assign a integer 123 to an int variable a.
- Convert the variable a to a String and store it in the variable s.
Ans.
- a. int a=123;
- String s=Integer.toString(a);
Q. 3 Answer as directed:
- Assign a string “123” to a String variable a.
- Convert the variable a to an integer and store it in the variable s.
Ans.
- a. String a=”123”;
- int s=Integer.parseInt(a);
Q. 4 For the program given below mark the parts ___1___ and __2__ as autoboxing or unboxing.
class Example
{
static void main()
{
Integer i;
int sum=0;
for(i=1;i<=10;i++) //___1___
{
sum=sum+i; //__2__
}
System.out.println(sum);
}
}
Ans.
__1__:Autoboxing
__2__:Unboxing
Q. 5 Give the output of the following program when the main( ) method is executed:
class Overload
{
static void wrapper(int a)
{
System.out.println(“int type=”+a);
}
static void wrapper(Integer a)
{
System.out.println(“Integer type=”+a);
}
static void main()
{
int b=13;
Integer c=new Integer(b);
wrapper(c);
wrapper(b);
}
}
Ans. Output:
Integer type=13
int type=13
Q. 6 Give the output of the following code:
String A=“26”, B=“100”;
String D=A+B+“200”;
int x=Integer.parseInt(A);
int y=Integer.parseInt(B);
int d=x+y;
System.out.println(“Result 1=”+D);
System.out.println(“Result 2=”+d);
Ans.
Output:
Result 1= 26100200
Result 2=126
Q. 7 Write the return type of the following library functions:
- isLetterOrDigit(char)
- parseInt()
Ans.
- boolean
- int
Q. 8 (i) int var = ‘A’;
What is the value of var?
(ii) Name the package that contains wrapper classes.
Ans.
(i) var will hold the ASCII value of ‘A’ which is 65.
(ii) java.lang package contains wrapper classes.