1) Incorrect Loop Termination:
#include <stdio.h>
int main() {
int i;
for (i = 5; i <= 0; i--) {
printf("%d ", i);
}
return 0;
}
2) Improper Use of Logical Operator:
#include <stdio.h>
int main() {
int x = 5;
int y = 10;
if (x = y) {
printf("x is equal to y");
} else {
printf("x is not equal to y");
}
return 0;
}
3) Incorrect Calculation:
#include <stdio.h>
int main() {
int x = 5;
int y = 2;
int result = x / y;
printf("Result: %d\n", result);
return 0;
}
4) Infinite Loop:
#include <stdio.h>
int main() {
int i = 0;
while (i < 10) {
printf("%d ", i);
}
return 0;
}
5) Array Index Out of Bounds:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i <= 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
C++
1) #include <iostream>
int main() {
int x = 5;
// Logical error: using assignment instead of equality
if (x = 10) {
std::cout << "x is 10." << std::endl;
} else {
std::cout << "x is not 10." << std::endl; // This block will be executed incorrectly
}
return 0;
}
2) #include <iostream>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// Logical error: accessing elements beyond the array bounds
for (int i = 0; i <= 5; ++i) {
std::cout << arr[i] << " ";
}
return 0;
}
3)#include <iostream>
int main() {
int i = 0;
// Logical error: condition that never becomes false
while (i < 5) {
std::cout << i << " ";
// Missing increment statement, causing an infinite loop
}
return 0;
}
4)#include <iostream>
int main() {
double num1 = 0.1;
double num2 = 0.2;
// Logical error: floating-point imprecision in comparison
if (num1 + num2 == 0.3) {
std::cout << "The sum is 0.3." << std::endl; // This condition might not be true due to
floating-point imprecision
} else {
std::cout << "The sum is not 0.3." << std::endl;
}
return 0;
}
Java :
1) public class StringComparison {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = new String("Hello");
// Logical error: incorrect string comparison
if (str1 == str2) {
System.out.println("Strings are equal."); // This condition might not be true due to
reference comparison
} else {
System.out.println("Strings are not equal.");
}
}
}
2) public class ArrayLengthMismatch {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
// Logical error: accessing array elements beyond the length
for (int i = 0; i <= 5; i++) {
System.out.print(numbers[i] + " "); // This will cause an
ArrayIndexOutOfBoundsException
}
}
}
3) public class LoopIncrement {
public static void main(String[] args) {
int sum = 0;
// Logical error: missing increment statement, causing an infinite loop
for (int i = 0; i < 5; /* missing increment statement */) {
sum += i;
}
System.out.println("Sum: " + sum);
}
}
4) public class SwitchCaseFallThrough {
public static void main(String[] args) {
int day = 2;
String dayType = "";
// Logical error: missing break statements, causing fall-through
switch (day) {
case 1:
dayType = "Weekday";
case 2:
dayType = "Weekend"; // This will be executed even if day is 1
break;
default:
dayType = "Unknown";
}
System.out.println("Day type: " + dayType);
}
}
JS
1)let num = "5";
// Logical error: incorrect equality check
if (num == 5) {
console.log("The number is 5."); // This condition might be true due to type coercion
} else {
console.log("The number is not 5.");
}
2)let numbers = [1, 2, 3, 4, 5];
// Logical error: accessing array elements beyond the length
for (let i = 0; i <= 5; i++) {
console.log(numbers[i]); // This will result in undefined for the index 5
}
3)let i = 0;
// Logical error: missing increment statement, causing an infinite loop
while (i < 5) {
console.log(i);
// Missing increment statement, causing an infinite loop
}
4)let username = "";
// Logical error: incorrect falsy value check
if (username) {
console.log("Username is provided."); // This condition might be true for an empty string
} else {
console.log("Username is not provided.");
}