What is the output of the following code segment?


1) Give the output of the following code segment? for(int i = 1; i<=2; i++){ for(int j=1; j<=3; j++){ for(int k=0; k<=2; k++)
2) Give the output of the following blocks of code (a to f): a. String test = Alabama; int sum = 0; for (int i=0; i < test.
int sum, i; for (sum= 0, i= 0; i < 6; i++) sum +=i; System.out.println(sum); Cut and paste it into a program, compile it and
int a= 1, b= 2, c=5, d= 4, e=1, i; if (a <b && (c== d | e< 5)) i=1; else if(b <c||(d>e && b== a)) i= 2; else i= 3; System.out

Answer

1) class Main { public static void main(String[] args) { for(int i = 1; i<=2; i++){ for(int j=1; j<=3; j++){ for(int k=0; k<=2; k++) System.out.print(“$”); System.out.print(” “); System.out.println(); } } } } Online Java IDE, and onl... and host your programs and apps... save a run share + new repl 99 talk Sign up Main.java B saved 2a) class Main { public static void main(String[] args) { String test = “Alabama”; int sum = 0; for (int i=0; i<test.length(); i++) if (test.charAt(i) == ‘a’) sum++; System.out.println(sum); } } Online Java IDE, and on... , and host your programs and apps... save run share + new repl to re sik soup 89 talk Sign up Main 2b) class Main { public static void main(String[] args) { int numbers[] = {2,3,5,7,9}; int i=0, sum=0; while (numbers[i]<6){ sum += numbers[i]; i+=2; System.out.println(sum); } } } Output: Online Java IDE, and onl... and host your programs and apps... save a run share + new repl 88 talk Sign up Main.java saving E2c) class Main { public static void main(String[] args) { int sum, i; for(sum= 0, i=0; i < 6; i++) sum +=i; System.out.println(sum); } } Output: Online Java IDE, and onl... and host your programs and apps... save run share + new repl 88 talk Sign up Main.java B saving. 2d) class Main { public static void main(String[] args) { int k = 6; int i = k* 1/3; switch (i) { case 1: k=0; case 2: k= 1; default: k=2; System.out.println (k); } } } Output: Online Java IDE, and on... and host your programs and apps... save a run shares + new repl 89 talk Sign up 1 Main.java saved2e) class Main { public static void main(String[] args) { int a= 1, b=2, c= 5, d= 4, e= 1, i; if(a<b && (c==d || e<5)) i=1; else if(b<c||(d>e && b==a)) i= 2; else i= 3; System.out.println(i); } } Output: Online Java IDE, and onl... and host your programs and apps... save @ run share + new repl 88 talk Sign up Main.java saving..

Leave a Comment