Test I Model Answer
Test I Model Answer
Ans. A method with variable length arguments (varargs) in Java can have zero or Explanation
multiple arguments. Variable length arguments are most useful when the number with syntax:
of arguments to be passed to the method is not known beforehand. They also 1M
reduce the code as overloaded methods are not required. Example:1M
Syntax of varargs:
The varargs uses ellipsis i.e. three dots after the data type. Syntax is as follows:
return_type method_name(data_type... variableName){}
a) Define a class student with int id and string name as data members and a 4M
method void SetData ( ). Accept and display the data for five students.
Ans. import java.io.*; Correct
class student syntax and
{ logic: 4M
int id;
String name;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
void SetData()
{
System.out.println("enter id and name for student");
id=Integer.parseInt(br.readLine());
name=br.readLine();
}
void display()
{
System.out.println("The id is " + id + " and the name is "+ name);
}
public static void main(String are[])
{
student[] arr;
arr = new student[5];
int i;
for(i=0;i<5;i++)
{
arr[i] = new student();
}
for(i=0;i<5;i++)
{
arr[i].SetData();
}
for(i=0;i<5;i++)
{
arr[i].display();
}
}
}
b) 4M
Explain the command line arguments. Write a program to accept two
numbers from command line and print sum of these two numbers.
c) Define a class circle having data members pi and radius. Initialize and display 4M
values of data members also calculate area of circle and display it.
Syntax:
switch(expression)
{
case value1:
block 1;
break;
case value2:
block 2;
break;
.
.
.
default:
default block;
break;
}
Example:
public class SwitchExample
{
public static void main(String[] args) {
int number=20;
switch(number){
case 10: System.out.println("You are in 10");break;
case 20: System.out.println("You are in 20");break;
case 30: System.out.println("You are in 30");break;
default:System.out.println("Not in 10, 20 or 30");
}
}
}