0% found this document useful (0 votes)
42 views

For Loop Interview Questions

The document contains examples of for loop code snippets and their expected outputs. It includes loops with basic increment/decrement, conditional logic, and various initialization/condition/update expressions. In total there are over 60 code examples and explanations provided.

Uploaded by

katarichallenge
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

For Loop Interview Questions

The document contains examples of for loop code snippets and their expected outputs. It includes loops with basic increment/decrement, conditional logic, and various initialization/condition/update expressions. In total there are over 60 code examples and explanations provided.

Uploaded by

katarichallenge
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

1. What is the output of the below code?

for(var i=1;i<=10;i++){
console.log(i)
}

Correct Answer: 1,2,3,4,5,6,7,8,9,10

2. What is the output of the below code?

var i=4;
for(var i;i<=14;i++){
console.log(i)
}

Correct Answer: 4,5,6,7,8,9,10,11,12,13,14

3. What is the output of the below code?

for(var i=1;i<=5;){
console.log(i)
}

Correct Answer: Infinite loop

4. What is the output of the below code?

for(var i=0;;i++){
console.log(i)
}

Correct Answer: Condition is not given.So it will go to the infinite loop

5. What is the output of the below code?

for(var i=-3;i<=3;i++){
console.log(i)
}

Correct Answer : -3,-2,-1,0,1,2,3


6. What is the output of the below code?

for(var i=-4;i<4;i++){
console.log(i)
}

Correct Answer: -4,-3,-2,-1,0,1,2,3

7. What is the output of the below code?

for(var i=10;i>=1;i--){
console.log("i")
}

Correct Answer: “10” times “i” will print

8. What is the output of the below code?

for(var i=10;i>=5;i++){
console.log(i)
}

Correct Answer: Infinite loop.Because every time condition is true.loop never stops.

9. What is the output of the below code?

for(var i=-45;i<=-40;i++){
console.log(i)
}

Correct Answer: -45,-44,-43,-42,-41,-40

10. What is the output of the below code?

for(var i=1;i<=10;i++){
console.log(i*i)
}

Correct Answer: 1,4,9,16,25,36,49,64,81,100


11. What is the output of the below code?

for(var i;i<5;i++){
console.log(i*i)
}

Correct Answer: Initial value not given.

12. What is the output of the below code?

for(var i=1;i<="6";i++){
console.log(i*4)
}

Correct Answer: 4,8,12,16,20,24

13. What is the output of the below code?

for(var i="1";i<6;i++){
console.log(i*2)
}

Correct Answer: 2,4,6,8,10

14. What is the output of the below code?

for(var i=1;i<=4;i++){
console.log("i*i")
}

Correct Answer: 4 times “i*4” will prints

15.What is the output of the below code?

for(var i=1;i<=5;i++){
console.log(i**3)
}

Correct Answer: 1,8,27,64,125


16. What is the output of the below code?

for(var i=1;i<=4;i--){
console.log(i*2)
}

Correct Answer: It will go infinite loop

17. What is the output of the below code?

for (var i = 38; i >= 30; i--) {


console.log(i)
}

Correct Answer: 38,37,36,35,34,33,32,31,30


18. What is the output of the below code?

for(var i=1;i<=5;i++){
console.log(i*4)
}

Correct Answer: 4,8,12,16,20

19. What is the output of the below code?

for(var i=1;i<=10;i++){
if(i%2===0){
console.log(i)
}
}

Correct Answer: 2,4,6,8,10

20. What is the output of the below code?

for(var i=20;i>=10;i--){
if(i%2===0){
console.log(i)
}
}
Correct Answer: 20,18,16,14,12,10

21. What is the output of the below code?

for(var i=10;i<=20;i++){
if(i%2===0){
console.log(i)
}
}

Correct Answer: 10,12,14,16,18,20

22. What is the output of the below code?

for(var i=1;i<=20;i=i-4){
console.log(i)
}

Correct Answer: Infinite loop

23.What is the output of the below code?

for(var i=0;i<=10;i=i+3){
console.log(i)
}

Correct Answer: 0,3,6,9

24. What is the output of the below code?

var i = "3";
var n = "10"
for (var i; i <= n; i+2) {
console.log(i)
}

Correct Answer: This code will not get any output Because condition is false.
25. What is the output of the below code?

for(var i=5;i<="10";i=i+2){
console.log(i)
}

Correct Answer: 5,7,9

26. What is the output of the below code?

var i="7";
var n="10";
for(var i;i>=n;i=i-1){
console.log(i)
}

Correct Answer: 7 (string)

27. What is the output of the below code?


for(var i=5;i==20;i=i+1){
console.log(i)
}

Correct Answer: The output will not get any output.Because condition is false

28. What is the output of the below code?

var i=10;
var n="10"
for(var i=10;i==n;i=i+1){
console.log(i)
}

Correct Answer: 10
29. What is the output of the below code?
for(var i=13;i<=20;i=i+1){
console.log(i)
}
Correct Answer: 13,14,15,16,17,18,19,20
30.What is the output of the below code?

for(var i=2;i<=20;i=i-4){
console.log(i)
}

Correct Answer: It will go the Infinite loop

31. What is the output of the below code?

var n="20"
for(var i=4;i<=n;i=i+4){
console.log(i)
}

Correct Answer: 4,8,12,16,20

32. What is the output of the below code?

for(var i=5;i<10;i=i+1){
console.log(i)
}

Correct Answer: 5,6,7,8,9

33. What is the output of the below code?

for(var i=10;i<10;i=i+2){
console.log(i)
}

Correct Answer: This will code execute.Because condition is false.

34. What is the output of the below code?

for(var i=3;i<10;i=i+2){
console.log(i)
}
Correct Answer: 3,5,7,9
35. What is the output of the below code?

for(var i=10;i != 20;i=i+2){


console.log(i)
}

Correct Answer: 10,12,14,16,18

36. What is the output of the below code?

var i=20;
var n="20";
for(var i=20;i === n;i=i+2){
console.log(i)
}

Correct Answer: This code will not execute.Because condition is false.

37. What is the output of the below code?

for(var i=24;i<30;i=i-1){
console.log(i)
}

Correct Answer: It will go into an infinite loop.

38. What is the output of the below code?

for(var i=80;i<=100;i=i+5){
console.log(i)
}

Correct Answer: 80,85,90,95,100

39. What is the output of the below code?

for(var i=10;i>=3;i=i-2){
console.log(i)
}
Correct Answer: 10,8,6,4
40. What is the output of the below code?

for(var i=20;i>16;i=i+4){
console.log(i)
}

Correct Answer: It will go Infinite loop

41. What is the reason for the below code?

for(var i=30;i>24;i=i+1){
console.log(i)
}

Correct Answer : Every time the “i” value increments by 1 the loop will iterate infinite times

42. What is the output of the below code?

for(var i=100;i>85;i=i-5){
console.log(i)
}

Correct Answer: 100,95,90

43. What is the reason behind the below code?

for(var i;i>54;i=i-1){
console.log(i)
}

Correct Answer: The code will not execute because initialisation is not there

44. What is the output of the below code?

var n="20"
for(var i=20;i==n;i=i-1){
console.log(i)
}

Correct Answer: 20
45. What is the output of the below code?

for(var i=20;i>=15;i=i+1){
console.log(i)
}

Correct Answer: It will go into an infinite loop.

46. What is the output of the below code?

for(var i=10;i>3;i=i-2){
console.log(i)
}

Correct Answer: 10,8,6,4

47. What is the output for the below code?

for(var i=-10;i<=10;i++){
console.log(i)
}

Correct Answer: -10,-9,-8,-7,-6,-5,....0,1,2,3,...10

48. What is the output of the below code?

for(var i=10;i>=-10;i--){
console.log(i)
}

Correct Answer: 10,9,8,7,6,5….0,-1,-2,-3.-4….-10

49. What is the output for the below code?

for(var i=-5;i<=10;i++){
console.log(i)
}

Correct Answer: -5,-4,-3,-2,-1,0,1,2,3,...10


50. What is the output for the below code?

for(var i=-5;i<=-2;i++){
console.log(i)
}

Correct Answer: -5,-4,-3,-2

51. What is the output of the below code?

for(var i=-6;i<=0;i++){
console.log(i)
}

Correct Answer: -6,-5,-4,-3,-2,-1,0

52. What is the output of the below code?

for(var i=-10;i<=0;i++){
console.log(i)
}

Correct Answer: -10,-9,-8,-7,-6,-5,-4.-3,-2,-1,0

53. What is the output of the below code?

for(var i=4;i>=-4;i--){
console.log(i)
}

Correct Answer: 4,3,2,1,0,-1,-2,-3,-4

54. What is the output of the below code?

for(var i=0;i<=10;i++){
if(i%2===0){
console.log(i)
}
}
Correct Answer: 0,2,4,6,8,10

55. What is the output for the below code?

for(var i=12;i>=4;i-2){
if(i%2===0){
console.log(i)
}
}

Correct Answer: 12,10,8,6,4

56. What is the output of the below code?

for(i=20;i<30;i--){
if(i%2===0){
console.log(i)
}
}

Correct Answer: It will go the Infinite loop

57. What is the output for the below code?

var n="10"
for(i=10;i==n;i++){
if(i%2===0){
console.log(i)
}
}

Correct Answer: 10

58. What is the output for the below code?

for(var i=0;i<=10;i++){
if(i%2!==0){
console.log(i)
}
}
Correct Answer: 1,3,5,7,9

59. What is the output for the below code?

for(var i=2;i<=14;i=i+2){
if(i%2!==0){
console.log(i)
}
}

Correct Answer: This code will not execute

60. What is the output for the below code?

for(var i=3;i<=14;i=i+2){
if(i%2!==0){
console.log(i)
}
}

Correct Answer: 3,5,7,9,11,13

61. What is the output for the below code?

for(var i=20;i<=31;i++){
if(i%2!==0){
console.log(i)
}
}

Correct Answer: 21,23,25,27,29,31

62. What is the output for the below code?

for(var i=40;i<=60;i++){
if(i%2!==0){
console.log(i)
}
}

Correct Answer: 41,43,45,47,49….59


63. What is the output of the below code?

for(var i=1;i<10;i++){
if(i%2!==0){
console.log(i)
}
}

Correct Answer: 1,3,5,7,9

64. What is the reason behind this code?

for(var i=1;i>10;i=i-1){
console.log(i)
}

Correct Answer: The code will Condition fail.The code will not execute

65. What is the output for the below code?

for(var i=10;i>=1;i=i-1){
if(i%2===0){
console.log(i)
}
}

Correct Answer: 10,8,6,4,2

66. What is the output for the below code?

for(var i=10;i>1;i=i-1){
if(i%2!==0){
console.log(i)
}
}

Correct Answer: 9,7,5,3


67. What is the output for the below code?

for(var i=100;i>80;i=i-1){
if(i%2===0){
console.log(i)
}
}

Correct Answer: 100,98,96,94,92,90…82

68. What is the output for the below code?

for(var i=95;i>85;i=i-1){
if(i%2!==0){
console.log(i)
}
}

Correct Answer: 95,93,91,89,87

69. What is the output for the below code?

for(var i=1;i<=10;i++){
console.log(i*4)
}

Correct Answer: 4,8,12,16,20,24,28,32,36,40

70. What is the output for the below code?

for(var i=1;i<=10;i++){
console.log(i*i)
}

Correct Answer: 1,4,9,16,25,36,49,64,81,100


71. What is the output of the below code?

for(var i=1;i<=10;i++){
console.log(i*2)
}
Correct Answer: 2,4,6,8,10,12,14,16…20

72. What is the output for the below code?

for(var i=10;i>=4;i=i-2){
console.log(i*2)
}

Correct Answer: 20,16,12,8

73. What is the output for the below code?

for(var i=10;i>=5;i=i-1){
console.log(i*4)
}

Correct Answer: 40,36,32,28,24,20

74. What is the Reason behind the below code?

for(var i=10;i<5;i=i+1){
console.log(i*4)
}

Correct Answer: Condition false.The code will not execute

75. What is the output of the below code?

for(var i=0;i<=10;i++){
if(i%2===0 && i%3===0){
console.log(i)
}
}

Correct Answer: 0,6

76. What is the output for the below code?

for(var i=10;i<=40;i++){
if(i%2===0 && i%3===0){
console.log(i)
}
}

Correct Answer: 12,18,24,30,36

77. What is the output for the below code?

for(var i=10;i<=40;i++){
if(i%4===0 && i%5===0){
console.log(i)
}
}

Correct Answer: 20,40

78. What is the output for the below code?

for(var i=20;i<=40;i++){
if(i%2===0 && i%4===0){
console.log(i)
}
}

Correct Answer: 20,24,28,32,36,40

79 .What is the Output for the below code?

for(var i=0;i<=10;i++){
if(i%2===0 || i%5===0){
console.log(i)
}
}

Correct Answer: 0,2,4,6,8,10


80 .What is the output for the below code?

for(var i=10;i<=20;i++){
if(i%2===0 || i%5===0){
console.log(i)
}
}
Correct Answer: 10,12,14,15,16,18,20

81. What is the output for the below code?

for(var i=20;i<=40;i++){
if(i%5===0 || i%10===0){
console.log(i)
}
}

Correct Answer: 20,25,30,35,40

82. What is the output for the below code?

for(var i=20;i<40;i++){
if(i%4===0 || i%15===0){
console.log(i)
}
}

Correct Answer: 20,24,28,30,32,36

You might also like