Question

Q.3.6 What does the following code output? int x = 8; int y = 2; while(x > 8) { X- y+=2; } System.out.println(y); Q.3.7 What

PLEASE SOLVE THESE QUESTIONS

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

Q-3.6: The output of the above program is 2
1. public class Main
{
2. public static void main(String[] args)
{
3. int x=8;
4. int y=2;
5. while(x>8){
6. x--; //decrement x
7. y+=2; //increment 2 }   
8. System.out.println(y); // print the value of y   
}}
In line 3 and line 4 declaring and initializing the variables x and y  i.e. x=8, y=2.
In line 5 we are checking the condition, whether x=8 is greater than 8 (i.e. 8>8) it is false so no change is made to the values of x and y( in line-6 and line-7) therefore come out of the loop and execute the statement in line-8 that is printing the value of y that is 2. Hence the output of the above program is 2.
The screenshot of the program and the output is shown below:

{ 8 1 //Question-3.6 2 public class Main 3- { 4 public static void main(String[] args) 5 6 //variable declaration and intiali

Output:

2

Q-3.7. The output of the given question is 10.

System.out.println((2+3-5)*(3/3)+(5+5));

If we have more than one operation in a single line, then we should follow the below rules:-

The expression inside the brackets '()' is evaluated first and then follow the below table.

Operator Associativity
++ -- !   Right to left(higher precedence)
* / %       Left to right
+ - Left to right
> >= < <= Left to right
== != Left to right
&&   Left to right
|| Left to right
= += -= *= /= %=   Right to left (least precedence)

Following the above rules the given expression is solved as follows:
(2+3-5)*(3/3)+(5+5) //Solve brackets first
= 0 * 1 + 10 // Next multiplication has higher precedence
= 0 + 10 // Finally addition is performed
= 10  
Therefore the given line prints the value 10.

Screenshot of the program along with the output is shown below:

1 //Question-3.7 2 public class Main 3- { 4. public static void main(String[] args) { 5 System.out.println((2+3-5)*(3/3)+(5+5

Output:

10

Q-3.8. The output of the given code is false.
1. public class Main{
2. public static void main(String[] args){
3. if((2==2)&&(3!=3)&&((5+1)==6)){
4.   System.out.println(true);
5. } else{   
6.   System.out.println(false); }
}}

In line-3 we are using the logical and operator (&&) for checking the if condition. The logical and (&&) evaluates to true only if both (or all the) operands are true, if any one of them is false, the condition becomes false.
Lets check the first operand 2==2 it's true, next check whether 3!=3 it is false, so the next operand is not checked since the if condition became false. Hence the body of the if statement(line-4) is not executed because the if condition is false. The body of the if statement (line-4) is executed only if the condition inside the brackets is true.
So come out of the loop and execute the else statement in line 6. Therefore, it prints false as the output.

Screenshot of the program along with the output is shown below:

{ 1 //Question-3.8 2 public class Main 3-{ 4 public static void main(String[] args) 5. { 6 if((2==2)&&(3!=3)&&((5+1)==6)) //

Output:

false

Add a comment
Know the answer?
Add Answer to:
PLEASE SOLVE THESE QUESTIONS Q.3.6 What does the following code output? int x = 8; int...
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
  • pic 4: Loops What is the output of the following code? int main0 ( int x...

    pic 4: Loops What is the output of the following code? int main0 ( int x = 1; while (x < 7) t İf ( (x % 2)-= 1)(1/ if x is odd x=x+1; else t x = x + 2; return 0; Select óne O a. 124 6 8 O b. 1 234 5 6 7 O c. 246 O d. 24 6 8 PREVIOUS PAGE NE

  • What is the output of the following code? for(int i =0; i<3; i++) { int x...

    What is the output of the following code? for(int i =0; i<3; i++) { int x = 0; x = x+i; Sy System.out.println(x);

  • What does the following code output? int x = 7, y = 10; X -= 30;...

    What does the following code output? int x = 7, y = 10; X -= 30; y *= 5; X += 32; y /= 4; cout << "y = " « y << " and x = " << x << "; " << endl; X -= 10; y /= 5; cout << "x = " << x < " and y = " << y « ";" << endl;

  • (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 {...

  • 20) What is the output of the following segment of C code: int avg(int n, int*...

    20) What is the output of the following segment of C code: int avg(int n, int* a); int main () {             int array[4]={1,0,6,9};             printf("%d", avg(4, array)+ 1);             system("pause");             return 0; } int avg(int n, int* a) { int i, sum=0; for (i=0;i<n;i++) { sum+=a[i]; } return sum/n; } a) 16 b) 5 c) 4 d) 8 21) What is the output of the following segment of C code: int x = 2; int y = 3;...

  • What is the output of the following code fragment? int i = 1; while( i <=...

    What is the output of the following code fragment? int i = 1; while( i <= 5 ) if(i == 2 || i == 4) System.out.println(i + ":" + " is an even index) System.out.println("i: " + i); i++;

  • 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...

  • JAVA 5) What is the output of the following code? int a = 70; boolean b...

    JAVA 5) What is the output of the following code? int a = 70; boolean b = false; if(a >= 70) { System.out.print(1); if(b==true) { System.out.print(2); } } else { System.out.print(3); if(b==false) { System.out.print(4); } } System.out.print(5); 6) What is the output of the code above using these initial values? int a = 43; boolean b = false; 7) The following method is SYNTACTICALLY correct (meaning it will compile). True or false? public boolean method() { int value = 5;...

  • QUESTION 1 What is the output of the following code snippet? int main() { bool attendance...

    QUESTION 1 What is the output of the following code snippet? int main() { bool attendance = false; string str = "Unknown"; attendance = !(attendance); if (!attendance) { str = "False"; } if (attendance) { attendance = false; } if (attendance) { str = "True"; } else { str = "Maybe"; } cout << str << endl; return 0; } False True Unknown Maybe QUESTION 2 What is the output of the following code snippet? #include <iostream> #include <string> using...

  • What is the output of the following C++ code? int count = 1; int num =...

    What is the output of the following C++ code? int count = 1; int num = 25; while (count < 25) { num--; count++; } cout << count « " " « num << endl i 25 1 0 24 1 0 25 0 0 24 0 In this while loop statement, while (counter < 10), where counter is an int variable. Which statement below is an equivalent way to write this while statement? while (10 < counter) while (9...

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