Question

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

   public static void main(String args[]) {

      int x = 45;

      int y = 10;

      if( x == 30 ) {

         if( y == 10 ) {

            System.out.print("X = 30 and Y = 10");

         else

            System.out.print("X = 45 and Y = 10");

         }

       }

       System.out.print("Nested If");

If the user enter 2000, what is the output of the code segment shown below?

public class Leapyear{

   public static void main(String[] args)   {

      int year;   

      Scanner console = new Scanner(System.in);

      System.out.print("Enter a year : ");

      year = console.nextInt();

      if (year % 4 == 0)      {

         if (year % 100 == 0) {

            if (year % 400 == 0) {

               System.out.println("A leap year");

            }

            else            {

               System.out.println("Not a leap year");

            }

         }

         else         {

            System.out.println("A leap year");

         }

      }

      else      {

         System.out.println("Not a leap year");

      }   } }

qstion Three

What is the output of:

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

                                {

                                                for (int j=1; j <= 5; j++)

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

                                                System.out.println();

  

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

Dear Student,

Here i have written complete Java code with the outputs KIndly Check it.

NOTE: Please note that all the program has been compiled and check using an online compiler.

Question No: 1

Program:a

---------------------------------------------------------------------------------------------------------------------------------------

public class Test

{
  
    public static void main(String []arg)
    {
      
    int x = 1;

    while (x != 15)

    {

    System.out.println ("Hello");

    x++;

    }


}

  
}

----------------------------------------------------------------------------------------------------------------------------------------

Output: Print Hello 14 times when the value of x is 15 program stops.below is the output.

Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello

----------------------------------------------------------------------------------------------------------------------------------------

Question No: 1

Program:b

----------------------------------------------------------------------------------------------------------------------------------------

public class Test

{
  
    public static void main(String []arg)
    {
      
    int i = 1;

    int sum = 0;

    while (i <= 5)

   {

   sum = sum + i;

   i++;

}

System.out.println("The value of sum is " + sum);


}

  
}

--------------------------------------------------------------------------------------------------------------------------------------

Output:2 The above program calculate the sum from 1 to 5 number and print the final result below is the output attached as a screen shot..

Output: The value of sum is 15

-----------------------------------------------------------------------------------------------------------------------------------------------

Question:2

Program:3

----------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Scanner;


public class Leapyear{

   public static void main(String[] args)   {

      int year;

      Scanner console = new Scanner(System.in);

      System.out.print("Enter a year : ");

      year = console.nextInt();

      if (year % 4 == 0)      {

         if (year % 100 == 0) {

            if (year % 400 == 0) {

               System.out.println("A leap year");

            }

            else            {

               System.out.println("Not a leap year");

            }

         }

         else         {

            System.out.println("A leap year");

         }

      }

      else    
      {

         System.out.println("Not a leap year");

      }
     
   }

}

---------------------------------------------------------------------------------------------------------------------------------------

If you run the above code you will get the output as given below..If input is 2000.

Output:

Input: 2000 > Run > Run-URL(Also Generates URL) Output: Enter a yearA leap year

So 2000 is a leap year.

--------------------------------------------------------------------------------------------------------------------------------------------

Question:2:

Program:1

-----------------------------------------------------------------------------------------------------------------------------------------

public class Test {

   public static void main(String args[]) {

      int x = 45;

      int y = 10;

      if( x == 30 ) {

         if( y == 10 ) {

            System.out.print("X = 30 and Y = 10");

         else

            System.out.print("X = 45 and Y = 10");

         }

       }

       System.out.print("Nested If");

----------------------------------------------------------------------------------------------------------------------------------------

Output: When you run the above program it results some syntax error. That are shown in the below attached screen shot.

Compile Errors and Warnings: 15: error else without if else 23: error: reached end Of fl↓e whi↓e parsınd System.out.print

----------------------------------------------------------------------------------------------------------------------------------------------

Question No:3

Program:1 below is the correct code snippet

---------------------------------------------------------------------------------------------------------------------------------------------

public class Test {

   public static void main(String args[]) {

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

    {

    for (int j=1; j <= 5; j++)
  
    System.out.print (i*j + " ");

    System.out.println();
    }
  
   }

}

---------------------------------------------------------------------------------------------------------------------------------------

Here i have attached the output as a screen shot...

Output:

----------------------------------------------------------------------------------------------------------------------------------------

Output: 1 234 5 246810 3 69 12 15 4 8 12 16 20 5 10 15 20 25

-------------------------------------------------------------------------------------------------------------------------------------------

KIndly Check and Verify Thanks...!!!

Add a comment
Know the answer?
Add Answer to:
(a)How many times does the code snippet given below display "Hello"? int x = 1; while...
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
  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int...

    what is output public static void main(String args) Scanner keyboard new Scanner(System.in); int u 14; int w 0; int x; int y 5; float z = 6.1 System.out.print("Enter y: "); x keyboard.nextint); System.out.println('y'); System.out.println(x); System.out.println(w*3); x- x+(int)z; System.out.println(x); 0 System.out.println(u); System.out.,println(u); System.out.println"x In" + y); System.out.print(y + z); ) liclosing main method 1 liclosing class header

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...

  • draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner;...

    draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner; public class Matrix {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);    System.out.print("Enter number of rows: first matrix ");    int rows = scanner.nextInt();    System.out.print("Enter number of columns first matrix: ");    int columns = scanner.nextInt();    System.out.print("Enter number of rows: seconed matrix ");    int rowss = scanner.nextInt();    System.out.print("Enter number of columns seconed matrix:...

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
Active Questions
ADVERTISEMENT