Review the following code:
public class Looping { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { System.out.println(i + " x " + j + " = " + (i * j)); } } } } |
Replace this text with your solution |
System.out.println(i + " + " + j + " = " + i + j); |
Replace this text with your solution |
Replace this text with your solution |
Ans
a)
answer of first part is: both loop will run from 1 to 5 and perform operation i*j so initially it starts with value of i from 1 and at i=1 j will iterate through j=1 to j=5 so it prints table of 1,2,3,4,5 till 5th term of each because second loop will iterate only five times
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25
Ans b) if we replace it with System.out.println(i + " + " + j + " = " + i + j); then it simply append the value of i and j while i vary from 1 to 5 for every j from 1 to 5.
for e.g at i=1 and for (int j = 1; j <= 5; j++) will execute so here j changes from 1 to 5(for every value of j in range 1 to 5)
so output of this is
1 + 1 = 11
1 + 2 = 12
1 + 3 = 13
1 + 4 = 14
1 + 5 = 15
it will give output for every i=1,2,3,4,5
so final output is:
1 + 1 = 11 1 + 2 = 12 1 + 3 = 13 1 + 4 = 14 1 + 5 = 15 2 + 1 = 21 2 + 2 = 22 2 + 3 = 23 2 + 4 = 24 2 + 5 = 25 3 + 1 = 31 3 + 2 = 32 3 + 3 = 33 3 + 4 = 34 3 + 5 = 35 4 + 1 = 41 4 + 2 = 42 4 + 3 = 43 4 + 4 = 44 4 + 5 = 45 5 + 1 = 51 5 + 2 = 52 5 + 3 = 53 5 + 4 = 54 5 + 5 = 55
Ans c) we get this particular result for part(b) because in print statement the logic i+j is written without parenthesis so compiler simply treat it as a string and perform append operation instead of performing addition operation.
compiler will try to solve first () parenthesis in the print() statement so here we are typecasting integer to string initially e.g i+"x" typecasting integer to string.
whenever we are passing arguements in System.out.println() by default toString() method will call so in that case it will typecast integer to string .
And in first part it perform multiplicative operation instead of append operation because compiler first simply (i* j) because of higher priority of () operator so it first simply i*j then it will append result of that with the string in the print statement.
for e.g (i + " + " + j +" = "+ i+j) will be (1 + " +" + 1 + " = " +1+1) =1 + 1 =11 (appending string)
Review the following code: public class Looping { public static void main(String[] args) { ...
Consider the following codes: public class TestThread extends Thread { public static void main(String[] args) { TestThread thread = new TestThread(); } @Override public void run() { printMyName(); } private void printMyName() { System.out.println("Thread is running"); } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...
import java.util.Scanner; public class TriangleMaker { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle."); Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); for (int i = 1; i <= size; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int...
Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } t is non-static and it cannot be referenced in a static context in the main method. The program compiles and runs fine. The variable t is not initialized and therefore causes errors. The variable x is not initialized and therefore causes errors.
What output is produced by the following program? public class MysteryNums public static void main(String[] args) - int x = 12; int y = x - 3; 3, sentence (y, x + y); . public static void sentence (int numi, int num2) { 4: System.out.println(num1 + " + num2);
What are the errors in this ? public class Mystery { public static void main(String[] args) { double initialSavings = 10000; double interestRate = 0.05; double currSavings = 0; int i; System.out.println("\nAnnual savings 5 years: "); currSavings = initialSavings; for (i = 0, i < 5, ++i); currSavings = (currSavings * interestRate); System.out.println("$" + currSavings); } }
Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } The variable t is private and therefore cannot be accessed in the main method. The program compiles and runs fine. t is non-static and it cannot be referenced in a static context in the main method. The variablet is not initialized and therefore causes errors. The variable x is not initialized and therefore causes errors.
c) public class Test { public static void main(String[] args) { T t1 = new T(); T t2 = new T(); System.out.println("t1's i = " + t1.i + " and j = " + t1.j); System.out.println("t2's i = " + t2.i + " and j = " + t2.j); } } class T { static int i = 0; int j = 0; T() { i++; j = 1; } } Why is t1's i = 2 ? It should be...
What is wrong the following code? Explain. public class Test { public static void main(String[] args) { A a = new A(5.0); } class A { int value = 2; } }
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(""); ]...
import java.util.Arrays; public class lab { public static void main(String args[]) { int arr[] = {10, 7, 8, 9, 1, 5,6,7}; int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; int arr3[] = {1, 3, 5, 3, 2, 6, 20}; quicksort(arr,0,arr.length-1); quicksort(arr2,0,arr2.length-1); quicksort(arr3,0,arr3.length-1); System.out.println(Arrays.toString(arr)); System.out.println(Arrays.toString(arr2)); System.out.println(Arrays.toString(arr3)); } private static int partition(int[] items,int low, int high) { int i=0; int j=0;...