-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCodeTracingExercises.java
More file actions
2212 lines (1993 loc) · 49.1 KB
/
Copy pathCodeTracingExercises.java
File metadata and controls
2212 lines (1993 loc) · 49.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
public class CodeTracingExercises {
// ===================== BASIC ARITHMETIC =====================
// 1. Basic Arithmetic
public int basicArithmetic() {
int x = 15;
int y = 4;
int z = x - y;
int result = z * 2;
return result;
}
// 2. Speed and Time
public int speedAndTime() {
int speed = 60;
int time = 3;
int distance = speed * time;
return distance;
}
// 3. Price Calculation
public int priceCalculation() {
int price = 25;
int quantity = 4;
int discount = 10;
int total = price * quantity - discount;
return total;
}
// 4. Temperature Conversion
public int temperatureConversion() {
int celsius = 20;
int fahrenheit = celsius * 9 / 5 + 32;
return fahrenheit;
}
// 5. Simple Division
public int simpleDivision() {
int a = 50;
int b = 10;
int c = a / b;
int result = c + 3;
return result;
}
// 6. Area Calculation
public int areaCalculation() {
int length = 12;
int width = 5;
int area = length * width;
int doubled = area * 2;
return doubled;
}
// 7. Wage Calculator
public int wageCalculator() {
int hours = 8;
int rate = 15;
int pay = hours * rate;
return pay;
}
// 8. Complex Arithmetic
public int complexArithmetic() {
int x = 7;
int y = 3;
int z = x + y;
int result = z * z;
return result;
}
// 9. Change Calculator
public int changeCalculator() {
int paid = 100;
int cost = 73;
int change = paid - cost;
return change;
}
// 10. Points Scored
public int pointsScored() {
int touchdowns = 3;
int fieldGoals = 2;
int points = touchdowns * 7 + fieldGoals * 3;
return points;
}
// ===================== FOR LOOPS =====================
// 11. Simple Loop
public int simpleLoop() {
int sum = 0;
for (int i = 1; i <= 4; i++) {
sum = sum + i;
}
return sum;
}
// 12. Count to Six
public int countToSix() {
int sum = 0;
for (int i = 1; i <= 6; i++) {
sum = sum + i;
}
return sum;
}
// 13. Even Numbers
public int evenNumbers() {
int sum = 0;
for (int i = 2; i <= 8; i += 2) {
sum = sum + i;
}
return sum;
}
// 14. Starting at Five
public int startingAtFive() {
int sum = 0;
for (int i = 5; i <= 9; i++) {
sum = sum + i;
}
return sum;
}
// 15. Multiplying Loop
public int multiplyingLoop() {
int sum = 0;
for (int i = 1; i <= 3; i++) {
sum = sum + i * 2;
}
return sum;
}
// 16. Countdown
public int countdown() {
int sum = 0;
for (int i = 5; i >= 1; i--) {
sum = sum + i;
}
return sum;
} // 15
// 17. Skip Counting
public int skipCounting() {
int sum = 0;
for (int i = 3; i <= 12; i += 3) {
sum = sum + i;
}
return sum;
} //30
// 18. Small Range
public int smallRange() {
int sum = 0;
for (int i = 10; i <= 12; i++) {
sum = sum + i;
}
return sum;
} //33
// 19. Zero Start
public int zeroStart() {
int sum = 0;
for (int i = 0; i <= 3; i++) {
sum = sum + i;
}
return sum;
} //6
// 20. Large Steps
public int largeSteps() {
int sum = 0;
for (int i = 5; i <= 20; i += 5) {
sum = sum + i;
}
return sum;
}
// ===================== CONDITIONALS =====================
// 21. Conditional
public int conditional() {
int age = 16;
int status;
if (age >= 18) {
status = 10;
} else {
status = 5;
}
return status;
}
// 22. Grade Check
public int gradeCheck() {
int score = 85;
int grade;
if (score >= 90) {
grade = 1;
} else {
grade = 2;
}
return grade;
}
// 23. Temperature Check
public int temperatureCheck() {
int temp = 72;
int comfort;
if (temp > 75) {
comfort = 20;
} else {
comfort = 10;
}
return comfort;
}
// 24. Speed Limit
public int speedLimit() {
int speed = 45;
int ticket;
if (speed > 55) {
ticket = 100;
} else {
ticket = 0;
}
return ticket;
}
// 25. Passing Grade
public int passingGrade() {
int score = 73;
int result;
if (score >= 70) {
result = 1;
} else {
result = 0;
}
return result;
}
// 26. Age Categories
public int ageCategories() {
int age = 25;
int category;
if (age >= 21) {
category = 3;
} else {
category = 2;
}
return category;
}
// 27. Balance Check
public int balanceCheck() {
int balance = 150;
int status;
if (balance < 100) {
status = 0;
} else {
status = 1;
}
return status;
}
// 28. Simple Comparison
public int simpleComparison() {
int x = 12;
int y;
if (x == 10) {
y = 5;
} else {
y = 15;
}
return y;
}
// 29. Discount Eligibility
public int discountEligibility() {
int purchase = 75;
int discount;
if (purchase >= 100) {
discount = 20;
} else {
discount = 5;
}
return discount;
}
// 30. Negative Check
public int negativeCheck() {
int value = -3;
int result;
if (value < 0) {
result = 0;
} else {
result = value;
}
return result;
}
// ===================== ARRAYS =====================
// 31. Array Access
public int arrayAccess() {
int[] nums = {5, 10, 15, 20};
int a = nums[1];
int b = nums[3];
int total = a + b;
return total;
}
// 32. First and Last
public int firstAndLast() {
int[] scores = {100, 85, 92, 78, 95};
int first = scores[0];
int last = scores[4];
int sum = first + last;
return sum;
}
// 33. Middle Elements
public int middleElements() {
int[] values = {3, 7, 11, 15};
int x = values[1];
int y = values[2];
int result = x * y;
return result;
}
// 34. Price List
public int priceList() {
int[] prices = {25, 30, 15, 40};
int item1 = prices[0];
int item2 = prices[2];
int total = item1 + item2;
return total;
}
// 35. Three Elements
public int threeElements() {
int[] data = {8, 12, 6, 10};
int sum = data[0] + data[1] + data[3];
return sum;
}
// 36. Distance Calculation
public int distanceCalculation() {
int[] miles = {50, 75, 100, 25};
int day1 = miles[1];
int day2 = miles[2];
int total = day1 + day2;
return total;
}
// 37. Subtraction Access
public int subtractionAccess() {
int[] nums = {100, 40, 60, 20};
int a = nums[0];
int b = nums[1];
int diff = a - b;
return diff;
}
// 38. All Elements
public int allElements() {
int[] arr = {2, 4, 6, 8};
int total = arr[0] + arr[1] + arr[2] + arr[3];
return total;
}
// 39. First Three
public int firstThree() {
int[] values = {15, 20, 25, 30, 35};
int sum = values[0] + values[1] + values[2];
return sum;
}
// 40. Index Calculation
public int indexCalculation() {
int[] nums = {5, 15, 25, 35};
int x = nums[2];
int y = nums[0];
int result = x - y;
return result;
}
// ===================== MODULO OPERATOR =====================
// 41. MOD Operation
public int modOperation() {
int n = 17;
int x = n % 5;
int y = x + 3;
return y;
}
// 42. Remainder Check
public int remainderCheck() {
int num = 23;
int rem = num % 7;
int result = rem * 2;
return result;
}
// 43. Division Remainder
public int divisionRemainder() {
int a = 50;
int b = a % 8;
int c = b + 10;
return c;
}
// 44. Mod and Add
public int modAndAdd() {
int x = 29;
int y = x % 4;
int z = y + y;
return z;
}
// 45. Large Modulo
public int largeModulo() {
int val = 100;
int mod = val % 9;
int out = mod + 5;
return out;
}
// 46. Time Calculation
public int timeCalculation() {
int minutes = 125;
int remainder = minutes % 60;
return remainder;
}
// 47. Double Mod
public int doubleMod() {
int n = 37;
int r1 = n % 10;
int r2 = r1 % 3;
return r2;
}
// 48. Mod Subtraction
public int modSubtraction() {
int x = 45;
int y = x % 7;
int z = 20 - y;
return z;
}
// 49. Small Divisor
public int smallDivisor() {
int num = 14;
int rem = num % 3;
int ans = rem * 5;
return ans;
}
// 50. Mod Chain
public int modChain() {
int a = 88;
int b = a % 11;
int c = b + 15;
int d = c % 4;
return d;
}
// ===================== WHILE LOOPS =====================
// 51. While Loop
public int whileLoop() {
int x = 2;
int count = 0;
while (x < 10) {
x = x * 2;
count = count + 1;
}
return count; //
} // In plain English: it counts how many times you need to double 2 before it reaches (or exceeds) 10.; 3
// 52. Counting Up
public int countingUp() {
int x = 1;
int count = 0;
while (x < 20) {
x = x + 4;
count = count + 1;
}
return count;
} //The method returns 5.In plain English: it counts how many times you can add 4 to 1 before reaching (or exceeding) 20.
// 53. Tripling
public int tripling() {
int x = 1;
int count = 0;
while (x < 30) {
x = x * 3;
count = count + 1;
}
return count;
}//4
// 54. Adding Five
public int addingFive() {
int x = 0;
int count = 0;
while (x < 15) {
x = x + 5;
count = count + 1;
}
return count;
}//3
// 55. Double Plus One
public int doublePlusOne() {
int x = 1;
int count = 0;
while (x < 25) {
x = x * 2 + 1;
count = count + 1;
}
return count;
}// 4
// 56. Large Threshold
public int largeThreshold() {
int x = 5;
int count = 0;
while (x < 50) {
x = x * 2;
count = count + 1;
}
return count;
}//4
// 57. Adding Three
public int addingThree() {
int x = 2;
int count = 0;
while (x < 20) {
x = x + 3;
count = count + 1;
}
return count;
}//6
// 58. Value Accumulator
public int valueAccumulator() {
int x = 1;
int sum = 0;
while (x <= 5) {
sum = sum + x;
x = x + 1;
}
return sum;
}// 15
// 59. Countdown While
public int countdownWhile() {
int x = 20;
int count = 0;
while (x > 5) {
x = x - 3;
count = count + 1;
}
return count;
}//5
// 60. Small Increments
public int smallIncrements() {
int x = 0;
int count = 0;
while (x < 10) {
x = x + 2;
count = count + 1;
}
return count;
}//5
// ===================== LOOPS WITH CONDITIONALS =====================
// 61. Counting Odds
public int countingOdds() {
int count = 0;
for (int i = 1; i <= 9; i++) {
if (i % 2 == 1) {
count = count + 1;
}
}
return count;
}//5
// 62. Counting Evens
public int countingEvens() {
int count = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
count = count + 1;
}
}
return count;
}
// 63. Multiples of Three
public int multiplesOfThree() {
int count = 0;
for (int i = 1; i <= 15; i++) {
if (i % 3 == 0) {
count = count + 1;
}
}
return count;
}//5
// 64. Greater Than Five
public int greaterThanFive() {
int count = 0;
for (int i = 1; i <= 12; i++) {
if (i > 5) {
count = count + 1;
}
}
return count;
}//7
// 65. Divisible by Four
public int divisibleByFour() {
int count = 0;
for (int i = 1; i <= 20; i++) {
if (i % 4 == 0) {
count = count + 1;
}
}
return count;
}
// 66. Less Than Ten
public int lessThanTen() {
int count = 0;
for (int i = 5; i <= 15; i++) {
if (i < 10) {
count = count + 1;
}
}
return count;
}
// 67. Odds in Range
public int oddsInRange() {
int count = 0;
for (int i = 10; i <= 20; i++) {
if (i % 2 == 1) {
count = count + 1;
}
}
return count;
}
// 68. Multiples of Five
public int multiplesOfFive() {
int count = 0;
for (int i = 1; i <= 25; i++) {
if (i % 5 == 0) {
count = count + 1;
}
}
return count;
}
// 69. Even Count
public int evenCount() {
int count = 0;
for (int i = 2; i <= 16; i++) {
if (i % 2 == 0) {
count = count + 1;
}
}
return count;
}// 8
// 70. Sum of Odds
public int sumOfOdds() {
int sum = 0;
for (int i = 1; i <= 7; i++) {
if (i % 2 == 1) {
sum = sum + i;
}
}
return sum;
}
// ===================== STRING LENGTH =====================
// 71. String Length
public int stringLength() {
String word = "PYTHON";
int len = word.length();
int result = len * 2;
return result;
}
// 72. Word Length
public int wordLength() {
String word = "JAVA";
int len = word.length();
int result = len + 5;
return result;
}
// 73. Name Length
public int nameLength() {
String name = "ALGORITHM";
int len = name.length();
int result = len * 3;
return result;
}
// 74. Short String
public int shortString() {
String text = "CODE";
int len = text.length();
int result = len * len;
return result;
}
// 75. String Calculation
public int stringCalculation() {
String word = "FUNCTION";
int len = word.length();
int result = len - 3;
return result;
}
// 76. Long Word
public int longWord() {
String word = "PROGRAMMING";
int len = word.length();
int result = len + len;
return result;
}
// 77. Variable Name
public int variableName() {
String word = "HELLO";
int len = word.length();
int result = len * 4;
return result;
}
// 78. Double Length
public int doubleLength() {
String text = "LOOPS";
int len = text.length();
int doubled = len * 2;
int result = doubled + 1;
return result;
}
// 79. Triple Length
public int tripleLength() {
String word = "IF";
int len = word.length();
int result = len * 3 + 2;
return result;
}
// 80. String Math
public int stringMath() {
String text = "ARRAY";
int len = text.length();
int result = len + 10;
return result;
}
// ===================== NESTED CONDITIONALS =====================
// 81. Nested Conditional
public int nestedConditional() {
int num = 12;
int val;
if (num > 10) {
if (num % 3 == 0) {
val = 15;
} else {
val = 10;
}
} else {
val = 5;
}
return val;
}
// 82. Grade Evaluation
public int gradeEvaluation() {
int score = 85;
int grade;
if (score >= 80) {
if (score >= 90) {
grade = 1;
} else {
grade = 2;
}
} else {
grade = 3;
}
return grade;
}
// 83. Age Category
public int ageCategory() {
int age = 25;
int category;
if (age >= 18) {
if (age >= 65) {
category = 30;
} else {
category = 20;
}
} else {
category = 10;
}
return category;
}
// 84. Number Check
public int numberCheck() {
int x = 8;
int result;
if (x > 5) {
if (x % 2 == 0) {
result = 100;
} else {
result = 50;
}
} else {
result = 25;
}
return result;
}
// 85. Temperature Status
public int temperatureStatus() {
int temp = 45;
int status;
if (temp > 32) {
if (temp > 70) {
status = 3;
} else {
status = 2;
}
} else {
status = 1;
}
return status;
}
// 86. Value Range
public int valueRange() {
int num = 7;
int code;
if (num < 10) {
if (num < 5) {
code = 1;
} else {
code = 2;
}
} else {
code = 3;
}
return code;
}
// 87. Score Bracket
public int scoreBracket() {
int score = 95;
int bracket;
if (score >= 90) {
if (score >= 95) {
bracket = 5;
} else {
bracket = 4;
}
} else {
bracket = 3;
}
return bracket;
}
// 88. Divisibility Check
public int divisibilityCheck() {
int num = 20;
int val;
if (num > 15) {
if (num % 5 == 0) {
val = 25;
} else {
val = 20;
}
} else {
val = 15;
}
return val;
}
// 89. Double Threshold
public int doubleThreshold() {
int x = 30;
int result;
if (x >= 20) {
if (x >= 40) {
result = 3;
} else {
result = 2;
}
} else {
result = 1;
}
return result;
}
// 90. Even or Odd Path
public int evenOrOddPath() {
int num = 14;
int path;
if (num % 2 == 0) {
if (num > 10) {
path = 8;
} else {
path = 4;
}
} else {
path = 2;
}
return path;
}
// ===================== ARRAY LOOPS =====================
// 91. Array Sum
public int arraySum() {
int[] data = {2, 4, 6, 8};
int total = 0;
for (int i = 0; i < data.length; i++) {
total = total + data[i];
}
return total;
}
// 92. Five Elements
public int fiveElements() {
int[] nums = {5, 10, 15, 20, 25};
int total = 0;
for (int i = 0; i < nums.length; i++) {
total = total + nums[i];
}
return total;
}
// 93. Small Array
public int smallArray() {
int[] values = {3, 7, 11};
int total = 0;
for (int i = 0; i < values.length; i++) {
total = total + values[i];
}
return total;
}
// 94. Sales Total
public int salesTotal() {
int[] sales = {100, 200, 150, 175};
int total = 0;
for (int i = 0; i < sales.length; i++) {
total = total + sales[i];
}
return total;
}
// 95. Score Sum
public int scoreSum() {
int[] scores = {88, 92, 85, 90};
int total = 0;
for (int i = 0; i < scores.length; i++) {
total = total + scores[i];
}
return total;
}
// 96. Large Array
public int largeArray() {
int[] data = {1, 2, 3, 4, 5, 6};
int total = 0;
for (int i = 0; i < data.length; i++) {
total = total + data[i];
}
return total;
}