0% found this document useful (0 votes)
45 views4 pages

Answers Worksheet Java

questions for java

Uploaded by

prachi.sk07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

Answers Worksheet Java

questions for java

Uploaded by

prachi.sk07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Fundamentals of Java

Q.1) JVM stands for ____________


a) Java Verify Machine(b) Java Virtual Machine (c) Java Vector Machine
(d) None of these
Ans Java Virtual Machine
Q.2) IDE stands for ______________
a) Integrated development environment (b) Integrated developer environment
c) Internal developer environment (d) Internal development environment
Ans Integrated development environment
Q.3) Java Netbeans IDE is _____________ software.
a) Close source (b) Open source (c) Middle source (d) None of the above
Ans Open source
Q.4) ++ is known as which operator.
a) Increment operator (b) decrement operator
(c) assignment operator (d) modulus
Ans Increment operator
Q.5) If a = 20 and b = 30 , what will be the value of a %= b;
Ans 20
Q.6) Identify the valid variable names from the following:
Total marks, int, marks$, marks4, marks_1
Ans. marks$ , marks4, marks_1
Q.7) What will be the value of rem after the execution of the following code snippet? Why
code = 2;
switch(code)
{
case 1 : rem = 8;
case 2 : rem = 9;
case 3 : rem = 10;
break;
case 4 : rem = 11;
break;}
Ans. 10
Q.8) Write a code in java using a for loop to display cube of all odd numbers between 1 to 10. Is
for an entry controlled or exit controlled loop.
Ans int n1;
for (n1 = 1; n1 <= 10; n1 - = 2)
{
System.out.println(“Cube is “ + n1* n1* n1);
}
For is an entry controlled loop.

Q.9) The following code has error(s). Rewrite the correct code.
int number = 1
while (number >= 10);
{
System.out.println(number);
Number = number + 1
}
Ans. int number = 1;
while (number <= 10)
{
System.out.println(number);
number = number + 1;
}
Q.10) Write the output of the code given below:
int x = 5, y = 7;
x *= y;
System.out.print(x);
System.out.println(y);
Ans. 35
7
Q.11) If a = 20 and b = 30 , what will be the result of i) a = = b (ii) a != b
Ans. i) False (ii) True
Q.12) Rewrite the following code using while loop:
int x = 10;
for(int i = 2; i<= 12; i+= 4)
{
System.out.println(x + i);
x = x – 2;
}
Ans. int x = 10;
int i = 2;
while ( i <= 12)
{
System.out.println(x + i);
x = x – 2;
i += 4;
}
Q.13) Rewrite the following code using for loop:
int n1 = 2;
while (n1 <= 10)
{
System.out.println(“Square is “ + n1 * n1);
n1 += 2;
}
Ans. int n1 ;
for (n1 = 2 ; n1 <= 10; n1 += 2)
{
System.out.println(“Square is “ + n1 * n1);
}
Q.14) How many times the loop will execute?
for(int number =12; number >=1; number = number – 4)
{ System.out.print(“Square of” + number);
System.out.println(“=” + number*number);
}
Ans. The loop will execute three times
Q.15) Find error and rewrite the correct code
i == 0;
while (i< 5)
{
System.out.println(i)
i++;
Ans. int i = 0;
while (i< 5)
{
System.out.println(i) ;
i++;
}
Q.16) The following code has error(s). Rewrite the correct code underlining all the corrections
made :
int n=15,
int i=10,x=1;
do;
{
x=x*i;
i++;
while[i<=n]
jTextField1.settext(""+x);

Ans. int n=15;


int i=10,x=1;
do
{
x=x*i;
i++;
} while (i<=n );
jTextField1.setText(""+x);

Q.17) Write statements for the following in java:


Declare a variable amount of integer type and store 2000 in amount variable. Increase the
amount by 5 % and display the final amount.
Ans. int amount = 2000;
amount = amount + 5.0/100 * amount;
System.out.println(amount);

Q.18) Write a statement to declare a variable length of type decimal and store 34.5 in it.
Declare another variable result of type decimal and store the square of length in it and
display the result.
Ans. double length = 34.5, result = 0;
result = length * length;
System.out.println( result );

Q.19) Rewrite the following code using if:


int digit = 11;
switch(digit)
{
case 1 :System.out.println(“Digit is one”);
break;
case 2 :System.out.println(“Digit is two”);
break;
case 3 :System.out.println(“Digit is three”);
break;
default :System.out.println(“Not in range”);
}
Ans. int digit = 11;
if (digit = = 1)
System.out.println(“Digit is one”);
else if (digit = = 2)
System.out.println(“Digit is two”);
else if (digit = = 3)
System.out.println(“Digit is three”);
else
System.out.println(“Not in range”);
Q.20) Write a program in java to calculate and display the area of circle, rectangle and triangle.
Area of circle
double radius = Double.parseDouble(jTextField1.getText());
System.out.println(“Area of cifrcle = “ + 3.14 * radius * radius);

Area of rectangle
int l, b;
l = Integer.parseInt(jTextField1.getText());
b = Integer.parseInt(jTextField2.getText());
System.out.println(“Area of rectangle = “ + l * b );

Area of Triangle

int base, height;


base = Integer.parseInt(jTextField1.getText());
height = Integer.parseInt(jTextField2.getText());
System.out.println(“Area of Triangle = “ + 0.5 * base * height );

Q.21) Write a program in java to display for each number in the range of 1 to 20 whether it is even
or odd.
int num;
for (num = 1 ; num <= 20; num++)
{
if (num % 2 = = 0)
System.out.println(num + “ is even”);
else
System.out.println(num + “ is odd”);
}

Q.22) Write a program to display even numbers and their sum in the range of 20 to 1.
int num = 20, sum = 0;
while (num >= 1)
{
System.out.println(num);
sum = sum + num;
num = num + 2;
}
System.out.println(“Sum = “ + sum);

You might also like