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 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); } }
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);
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.
What is the output of the following code: public static void main(String []args){ int x = 10; int y = 10; try{ System.out.println(function1(y, x)); } catch (Exception e) { System.out.println(“ERROR”); } } static int function1 (int x, int y) { x += 10; y += 7; return (x + y); }
import java.util.Scanner; public class SieveOfEratosthenes { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int num = sc.nextInt(); boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false;...
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...
Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next(); String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word); if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...