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

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

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

  • 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

  • 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();      ...

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

  • Hello, can someone please fix my code? It runs perfect but I'm having trouble with the...

    Hello, can someone please fix my code? It runs perfect but I'm having trouble with the index for the incorrect answer array; please run some tests to ensure the code runs properly, thank you! Here is the code: ___________________________________________________________________________________________________________________________ import java.util.Scanner; public class DriverTest{    final static int QuestionTotal = 10;    public static void main(String[] args){          // scanner and answer key        Scanner scan = new Scanner(System.in);        final char[] answers = {'A', 'B', 'C',...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • JAVA: Rewrite the following code to where the inputs are from a file. The file name...

    JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; }    public static void main(String[] args) { int n, i, k; System.out.println("Enter...

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

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