Question

Java 1. Can you explain it how it will be printed step by step? Thanks!! What...

Java

1. Can you explain it how it will be printed step by step?

Thanks!!

What is printed by the following?

for (int i=1; i<4; i++) { for (char c=’a’; c<=’c’; c++) {

if (i%2==0) { i++; System.out.println(i + " " + c); }

else { c++; System.out.println(c + " " + i);

} } }

2. Is there a compiler error in this code? If so, what is it? If not, what’s printed?

Is there a compiler error in this code? If so, what is it? If not, what’s printed?

1 public class WhatsPrinted10 {

public static void f1() {

System.out.print("a");

}

public static void f2() {

f1();

System.out.print("b");

}

public static void f3() {

f2();

f1();

System.out.print("c");

}

public static void main(String args[]) {

f3();

System.out.println();

}

}

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

1)The outer loop will run for 4 times

The inner loop will have intial value as 'a' which will be incremented till c becomes 'c' so depending the if condition the statements will be printed the result will be:

phpEDBVRB.png

2)There is no compilation error in given code everything is fine it will be executed successfully the output will be:

From the main method the call stack will be to f2,f3 and f1 methods which will print the statements as:

abac

Add a comment
Know the answer?
Add Answer to:
Java 1. Can you explain it how it will be printed step by step? Thanks!! What...
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
  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

  • Java i know that result will be 415. Can you explain how it works. step by...

    Java i know that result will be 415. Can you explain how it works. step by step please. thanks public static void whatsPrinted(int A[]) { for (int i=1; i<A.length; i++) { A[i]=A[i-1]*2+1; } } public static void main(String args[]) { int A[] = {12,3,8,9,7,11}; whatsPrinted(A); System.out.println(A[A.length-1]); }

  • **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args)...

    **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args) for (int k-1: k10:k if (k8) continue: System.out.println k) Question 2 3.5 pts Analyze the following code. Which one is correct? package p public class A package p2: import p1. public class B extends Af int i- 5 private int j- 10: public A0I public static void main(Stringll args) B b new B0: System.out.println(a.i+", "+ajt", "+b.it""+bj): a.i is printable None of them b.i is...

  • What are the first five lines printed from this code and why? public class LoopQ {...

    What are the first five lines printed from this code and why? public class LoopQ { public static void main(String[] args) { for(char c = 'A'; c < 'Z'; c++) { for(int i = 0; i < 2; i++) { System.out.println("seat "+c+i); } } } }

  • Write a program that prints an imitation of the Composition II in Red, Blue, and Yellow...

    Write a program that prints an imitation of the Composition II in Red, Blue, and Yellow created in 1930 by Piet Mondrian painting, illustrated below. Use character sequences such as @@a or: to indicate different colors and use - and| to form the black lines. with algorithm public class Solution - public static void main(String[] args) { for(int FI ;İ<-50;İ++){ for(int Hj<-50;jt+){ if(j-12) { System.out.print("! "); } else ifj>12 && і 39){ System.out.print("@ "); 3 else if(i--39)\ System.out.print( "-"); }...

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

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

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • What is printed by running the following code? public static void main(String[] args) { int[] nums...

    What is printed by running the following code? public static void main(String[] args) { int[] nums = {2, 3, 4}; int n = 5; changeMe1(n, nums); System.out.print( n ); System.out.print(nums[0]); changeMe2(n, nums); System.out.print( n ); System.out.print(nums[0]); } public static void changeMe1(int number, int[] list) { number++; list[0]++; } public static void changeMe2(int number, int[] list) { number = 9; list = new int[1]; list[0] = 99; }

  • Java questions QUESTION 8 Assume the following classes are each defined in their own java files...

    Java questions QUESTION 8 Assume the following classes are each defined in their own java files and compile public class A public void display { System.out.println("A's display"); public class B extends A{ public void display { System.out.println("B's display"); public class C extends A public void display { System.out.println("C's display"); public class D { public static void main(String args) { 3: int i = ((int)(Moth.random(* 10)) if (i == 0) a = new BO: else if (i == 1) a =...

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