RevisionTerm1 QP
RevisionTerm1 QP
Q4. What is the final value of ctr after the iteration process given below, executes?
int ctr=0;
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j+=2)
++ctr;
Q5. How many times will the following loop execute? What value will be returned?
int x = 2, y = 50;
do {
++x;
y-=x++;
}while(x<=10);
return y;
Q6. Analyse the given program segment and answer the following questions:
(i) Write the output of the program segment
(ii) How many times does the body of the loop gets executed?
for(int m=5; m<=20; m+=5)
{
if(m%3 == 0)
break;
else
if(m%5 == 0)
System.out.println(m);
continue;
}
Q7. Rewrite the following segment in ternary operator.
1) if(marks>40&&marks<=100)
Grage= ‘A’;
else
Garde=’F’;
2) if (bill<10000 )
discount = bill * 10.0/100;
else
discount = bill * 5.0/100;
Q8. Use switch case statement for the given program snippet:
if(x = = 10)
a=r*2;
if(x = = 20)
a=r*4;
if(x = = 30)
a=r*6;
if(x = = 40)
a=r*8;
Q9. Convert following do-while loop into for loop.
int i=1; int d=5;
do{
System.out.println(i);
i++;
}while(i<=5);
Q10. The following function is a part of some class. It returns the value 1 when the number is an
Armstrong number, otherwise it returns 0. /* An Armstrong number is a number which is equal to
the sum of the cube of its individual digits*/
class Armstrong
{
int arms(int n)
{
int digit=0, sum=0;
int rem=n;
while(?1?)
{
digit=?2?;
sum=sum+?3?;
rem=?4?;
}
if(?5?)
return 1;
else
return 0;
}
}
i) What is the expression / value at ?1?
ii) What is the expression / value at ?2?
iii) What is the expression / value at ?3?
iv) What is the expression / value at ?4?
v) What is the expression / value at ?5?
Q11. Write a Java program to input a sentence from the user in lowercase and removes the first and
the last characters of every word in it.
Sample Input : i love java for school.
Sample Ouptut : ov av o choo
Some of the data members and member functions are given below:
Class name : Remove
Data members/instance variables:
sent : stores the sentence
rem : to store the new sentence
size : stores the length of the sentence
Member functions:
Remove() : default constructor
void readsentence() : to accept the sentence
void remfirstlast() : extract each word and remove the first and the last alphabet of the word
and form a new sentence using the changed words
void display() : display the original sentence along with the new changed sentence.
Specify the class Remove giving details of the constructor Remove (), void readsentence(), void
remfirstlast() and void display() . Define the main() function to create an object and call the function
accordingly to enable the task.
Q12. Design a class Perfect to check if a given number is a perfect number or not. [ A number is said
to be perfect if sum of the factors of the number excluding itself is equal to the original number]
Example : 6 = 1 + 2 + 3 (where 1, 2 and 3 are factors of 6, excluding itself) Some of the members of
the class are given below:
Design a class Special to check if the given number is a Special number or not. Some of the members
of the class are given below:
Member functions :
boolean isSpecial( ) : checks for the special number by invoking the function factorial(
) and returns true if Special, otherwise returns false
Specify the class Special, giving details of the Constructor, void read( ), int factorial(int), boolean
isSpecial( ) and void display( ). Define the main() function to create an object and call the member
function according to enable the task.
***************************