Driver Code : (in java)
public class Main
{
public static void main(String[] args) {
for(int j=0;j<7;j=j+2)
{
for(int k=j;k<4;k++)
{
System.out.print("hey");
}
System.out.println(".");
}
}
}
Output :
Explanation :
step 1 : Here, when the control enters the first for loop initially j=0 and here it satisfies the condition j<7 and j will be incremented to 2 and as it satisfies the condition it enters into the second for loop and as of the given condition here it initializes k to j that is k=0 and as 0<4 satisfies the condition so it enters into the loop and prints "hey" and here as k will be incremented to 1 and as again it satisfies the given condition it will print "hey" and it will print "hey" until the condition is false so until k is 4 it will print "hey".Therefore, it will print "heyheyheyhey" until it exits the first for loop and then and it will exit the loop and it will print "." and here it will print "heyheyheyhey."
step 2 : Here as the given function is "println" so here the next statement will print after a "line" .So, here it again enters into the loop and here initially j=2 and here j also satisfies the condition j<7 and again it will again enter into the second for loop here k will be 2 and here as 2<4 so it will print "hey" until the condition k<4 is false .So,it prints "heyhey." and then exists the two loops
step 3 : Here j will be 4 as j=j+2 initially and here as it enters the condtion k<4 doesn't satisfy the condition so it will not enter into the second for loop so here it prints only "."
step 4 : Here j will again increment to 6 and here it again prints "." as it doesn't enter the second for loop and here now j will be 8 as its doesn't enter the first loop because j=8 doesn't satisfy the condition j<7 .So, finally it will exit from the loop by printing the above lines .Thus it will print the output lines.
For any further queries please leave a comment below.
how and why is this the answer as well? thank you 35. What is the output...
Please answer both questions thank you.
Question 52 (1 point) What is the output of the code snippet given below? String s = "abcde"; int i = 1; while (i < 5) { System.out.print(s.substring(i, i + 1)); i++; Question 50 (1 point) How many times will the output line be printed in the following code snippet? Please enter your answer as an integer. for (int i = 0; i < 3; i++) { for (int j = 1; j <...
What is the output?
select the correct answer
What will be the output at line 13? 1 #include <iostream> 2 using namespace std; 5 int main() 6 int k-4; 8 for(int j-3; j<3+11; j++) 92 { 10 k-k+3; 11 12 13 cout<<k3 14 15 return; 16 7 Select one: A. 44 B. 23 C. 26 D. 27
QUESTION 8 What is the exact output of the following nested loop? for (int row = 2; row<10; row += 4){ int column=1; while (column <= 4){ System.out.print(row - column + ""); column += 2; System.out.println(); wo- 1-153 1-1 53 All of these are correct.
What is the output of the following code? for(int i =0; i<3; i++) { int x = 0; x = x+i; Sy System.out.println(x);
What is the output of the following code fragment? int i = 1; while( i <= 5 ) if(i == 2 || i == 4) System.out.println(i + ":" + " is an even index) System.out.println("i: " + i); i++;
Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...
What is the output of the following? int sum=3, z=1; do { printf("The sum:%d*", sum+z); } while (sum<= 1); printf("0"); * 1 (2 Points) 40 4
Consider the following code segment. int[]arr={1, 2, 3, 4, 5, 6, 7, 8}; for(int k=3; k<arr.length-1; R++ arr[k]-arr[k+1]; What are the contents of arr as a result of executing the code segment? a. {1, 2, 3, 5, 6, 7, 8, 8) b. {2, 2, 4, 5, 6, 7, 8, 8} C. {2, 4, 6, 5, 6, 7, 8,8} d. {4, 2, 4, 4, 6, 7, 8, 8) e. {6, 6, 4, 5, 6, 7, 8, 8} int) arr={7, 2.5, 3.0,...
Please explain how you get the output for each case.
3. What is the output of the following jave code fragments: a. intl A-(1,3,0,2,4) int temp = A[0]; for (int í=0; i< A.length-l; i++) A(幻-A(1+1); AI4]-temp; for (int i: A) System.out.print(ALi]" b. intll A-(3,0,2,4,1) int B new int [51: for(int i-o; ǐ< A.length; ǐ++) for (int i: B)System.out.print (B[i]"" C. int A A-new intl for (int i-0: i A.length-1; 1++) AI4] -temp; for (int i: A)System.out.print (A[ +"