Question

Please try to explain the answers as well. It will be highly appreciated. Output of the...

Please try to explain the answers as well. It will be highly appreciated.

  • Output of the following expression:

5 * 4 % 2 = ?

  • Output of a loop:
    • Example: What will be the output of the Java code (Assume all variables are properly declared.)

num = 10;

while (num <= 32)

                num = num + 5;

System.out.println(num);

  • What will be the output of the Java code (Assume all variables are properly declared.)

public class test

{

   public static void main(String[] args)

   {

      int n1 = 8;

      int n2 = 3;

      double answer = (double ) n1 / n2;

      System.out.println(“The answer is “ + answer);

   }

}

Note: Check with and without type casting.

  • What will be the output of the Java code (Assume all variables are properly declared.)

for (int i = 1; i <= 3; i++)

{

for (int j = 2; j <= 3; j++)

{

                                                System.out.println(I + ”,”+ j);

                }

}

Answer:

  • Question:

Suppose x = 19.5. What will be the output of the statement

System.out.println((int)(x) % 3);

  • What will the output of the following Java code (Assume all variables are properly declared and the input is 3 4 –1).

num = console.nextInt();

sum = num;

while (num != -1)

{

     num = console.nextInt();

     sum = sum + num;

}

Answer:

  • What is the output of the following code Fragment?

int i =10;

while(true)

{

                i = i-1;

      if(i<= 6)

break;

System.out.print(i+ " ");

}

Answer:

  • What is the output of the following Java code:

int alpha = 5;

int beta = 4;

switch (beta)

{

case 2:

   alpha = alpha + 2;

case 4:

   alpha = alpha + 4;

   break;

case 6:

   alpha = alpha + 6;

default:

    alpha = alpha + 10;

}

System.out.print(alpha);

a.            7              c.             11

b.            9             d.            15

  • What is the output of the following code?

int i;

int result = 5;

for (i = 0; i <= 2; i++)

{

   result = result + 2;

   System.out.print(result + " ");

}

Write a line of code to call a method named printMe. The method has no return value and no arguments.

Find one error:

if (answer = male)

{

     //comment

}

else (answer == female)

{

   //comment

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

5 * 4 % 2 = 0
Evaluation process will start from left. * has higher precedence compared to % so first it will multiply 5 and 4
=20 % 2 = 0

*******************************************************************************

        int num = 10;
        while (num <= 32)
                num = num + 5;
        System.out.println(num);

It will print 35
In first iteration num will be 10+5=15 which is less than 32
In second iteration num will be 15+5=20 which is less than 32
In third iteration num will be 20+5=25 which is less than 32
In fourth iteration num will be 25+5=30 which is less than 32
In fifth iteration num will be 30+5=35 which is no less than 32. So it will exit from while loop and print 35


*******************************************************************************


It will print
The answer is 2.6666666666666665
Here, in line
          double answer = (double ) n1 / n2
i is getting converted to double so it will be 8.0/3, so it will return result in decimal which will be 2.6666666666666665


*******************************************************************************


Output will be
1,2                                                                                                                                          
1,3                                                                                                                                          
2,2                                                                                                                                          
2,3                                                                                                                                          
3,2                                                                                                                                          
3,3

Here, i is iterating 3 times and for each I, j is iterating 2 times and printing 2 and 3 for each i.


*******************************************************************************

Output will be 1.

Here in line
(int)(x) % 3
x is getting typecast with int so x will became 19 and 19%3 will be 1


*******************************************************************************


num = console.nextInt();
sum = num;
while (num != -1)
{
     num = console.nextInt();
     sum = sum + num;
}

It will take input from user and add it to variable sum. It will stop taking input when user enters -1
So, it will print sum of all the values entered by user except last entry of -1


*******************************************************************************


int i =10;
while(true)
{
                i = i-1;
      if(i<= 6)
break;
System.out.print(i+ " ");
}
It will print 9 8 7
First time, i=10-1=9 which is >=6 so it will print 9
Second time, i=9-1=8 which is >=6 so it will print 8
Third time, i=8-1=7 which is >=6 so it will print 7
Fourth time, i=7-1=6 which is <=6 so break and exit from program


*******************************************************************************


It will print 9

Here in switch case 4 will pass and it will go to the case  
   alpha = alpha + 4;
   alpha = 5 + 4;
   alpha = 9


*******************************************************************************


it will print 7 9 11

Initially value of result = 5
Loop will run 3 time from 0 to <=2
First time result = result + 2
       = 5 + 2 = 7
Second time result = result + 2
       = 7 + 2 = 9
Third time result = result + 2
       = 9 + 2 = 11
So it will print 7 9 11


*******************************************************************************

This is the method named printMe which has no return value and no arguments.

void printMe()
       {
                        //your code here
       }


*******************************************************************************


Line else (answer == female) is having an error

We can not write any condition in else statement. Correct code is

if (answer = male)
{
        //comment
}
else
{
    //comment
}

Add a comment
Know the answer?
Add Answer to:
Please try to explain the answers as well. It will be highly appreciated. Output of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • What is the output from each of the following segments of C++ code? Record the output...

    What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 25; j > 16; j -= 3)        cout << setw(5) << j;     Answer: 2. int sum = 0;       for (int k = -2; k <= 2; k++)         sum = sum +...

  • how and why is this the answer as well? thank you 35. What is the output...

    how and why is this the answer as well? thank you 35. What is the output of the following code? for (int j = 0; j < 7; ;=j+2) { for (int k = j; k < 4; k++) System.out.print("hey"); { System.out.println(".");

  • Class: C++ Final Exam Dates Multiple Choice Identify the choice thar best completes the statement or...

    Class: C++ Final Exam Dates Multiple Choice Identify the choice thar best completes the statement or answer the question N 1. In s tructures, the computer repeats particular statements a certain number of times depending on some condition(s) a. looping C. selection b. branching d. sequence 2. What is the output of the following CH code? count = 1; num - 25; while (count < 25) numnum - 1: count++; cout << count << " " << num << endl;...

  • Multiple Choice Identify the choice that best completes the statement or answers the que 1. Which...

    Multiple Choice Identify the choice that best completes the statement or answers the que 1. Which of the following is a valid Java identifier? Spay b. 4myGrade! c. newGrade! d. 1dollar static final int EndVal-1: int double; int num console.nextInt(); while (num != Endval) double num * 2; System.out.println (double); num console.next1nt(); 2. The above code is an example of a(n)--while loop. a. flag-controlled b. counter-controlled c. EOF-controlled d. sentinel-controlled 3. The length of the string "first java program" is:...

  • Please answer both questions thank you. Question 52 (1 point) What is the output of the...

    Please answer both questions thank you. Question 52 (1 point) What is the output of the code snippet given below? String s = "abcde"; int i = 1; while (i < 5) { System.out.print(s.substring(i, i + 1)); i++; Question 50 (1 point) How many times will the output line be printed in the following code snippet? Please enter your answer as an integer. for (int i = 0; i < 3; i++) { for (int j = 1; j <...

  • 1. What is the output of the following code segment? int array[] = { 8, 6,...

    1. What is the output of the following code segment? int array[] = { 8, 6, 9, 7, 6, 4, 4, 5, 8, 10 }; System.out.println( "Index Value" ); for ( int i = 0; i < array.length; i++ ) System.out.printf( "%d %d\n", i, array[ i ] ); 2. What is the output of the following code segment? char sentence[] = {'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u' }; String output = "The sentence...

  • Write a program that will check a Java file for syntax errors. The program will ask...

    Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....

  • Please explain if the following code is actually correct. If the following code correct, please explain...

    Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard.   Chapter 5 Exercise 37: Java Programming * * (Decimal to...

  • Please help me with this Java project. I'm trying to create a loop so if user...

    Please help me with this Java project. I'm trying to create a loop so if user enter value > 12, will prompt them a message and take them back to "How many elements do you wan to enter?". Thanks public static void main(String[] args) {        Scanner sc = new Scanner(System.in);           int i, j;       System.out.print("\n How meny elements do you want to enter? (N should be no more than 12) ");    int num...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT