wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
var x int
var x int
Correct.
Go
cin >> a;
int a; cin >> a;
Declare variable.
C++
let s1 = String::from("hello"); let s2 = s1; println!("{{}}", s1);
let s1 = String::from("hello"); let s2 = s1.clone(); println!("{{}}", s1);
Clone to avoid move.
Rust
for (int i=0; i<82; i++) {{}}
for (int i=0; i<82; i++) {{}}
Correct.
Java
def foo(): print('data')
def foo(): print('data')
Indent function body.
Python
void main() {{ print('data') }}
void main() {{ print('data'); }}
Add semicolon.
Dart
values[11]
if (values.indices.contains(11)) values[11]
Check index.
Kotlin
<input type='text' value='message'>
<input type='text' value='message' name='value'>
Add name attribute.
HTML
int list[96]; list[96]=5;
int list[96]; if(96<96){{}} else list[96]=5;
Bounds check.
C++
<center>value</center>
<div style='text-align:center;'>value</div>
Use CSS.
HTML
class = 'test'
class_name = 'test'
'class' is a keyword.
Python
if (count = 66)
if (count == 66)
Use ==.
Scala
int* p = nullptr; *p=5;
int* p = new int; *p=5;
Allocate memory.
C++
items(16)
if length(items) >= 16, items(16), end
Check length.
MATLAB
let msg = String::from("test"); let r=&msg; msg.push_str("!");
let mut msg = String::from("test"); let r=&msg; println!("{{}}", r); msg.push_str("!");
Cannot mutate while borrowed.
Rust
DELETE FROM items WHERE name=8
DELETE FROM items WHERE name=8;
Add semicolon.
SQL
if (data = 31)
if (data == 31)
Use ==.
R
int num = 'data';
String num = 'data';
Type mismatch.
Dart
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
jwt.sign({{id:1}}, 'key');
jwt.sign({{id:1}}, 'key', {{expiresIn:'2h'}});
Add expiration.
Node.js
p {{ color: green }}
p {{ color: green; }}
Add semicolon.
CSS
String a = 'output';
String a = "output";
Double quotes.
Java
name: output title: test,
name: output title: test
Remove comma.
YAML
'67' + 45
67 + 45
Avoid string coercion.
JavaScript
for (bar in data)
for (bar of data)
for...in iterates keys.
JavaScript
div {{ color=green; }}
div {{ color: green; }}
Use colon.
CSS
cin >> b cout << b;
cin >> b; cout << b;
Add semicolon.
C++
disp('value')
disp('value')
Correct.
MATLAB
def baz(count): return count + 1
def baz(count): return count + 1
Correct.
Python
class Item {{ int num; }};
class Item {{ public: int num; }};
Make public.
C++
{{"title":"output" "title":40}}
{{"title":"output", "title":40}}
Add comma.
JSON
h1 {{ font-size:71px color:green; }}
h1 {{ font-size:71px; color:green; }}
Add semicolon.
CSS
random.sqrt(100)
import random random.sqrt(100)
Import module first.
Python
int[] data = new int[61]; data[61] = 5;
int[] data = new int[61]; if (61 < data.length) data[61] = 5;
Check bounds.
Java
{{'name':'message'}}
{{"name":"message"}}
Use double quotes.
JSON
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
if (data = 40) {{}}
if (data === 40) {{}}
Use === for equality.
JavaScript
<note name='world'/>
<note name="world"/>
Double quotes.
XML
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
<img src='world.jpg'>
<img src='world.jpg' alt='desc'>
Add alt text.
HTML
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
<div><p>test</div></p>
<div><p>test</p></div>
Nest properly.
HTML
.Person {{ color: #333; }}
.Person {{ color: #333; }}
Correct.
CSS
let mut bar=12; let r1=&mut bar; let r2=&mut bar;
let mut bar=12; {{ let r1=&mut bar; }} let r2=&mut bar;
Only one mutable borrow.
Rust
UPDATE items SET name='hello' WHERE status=45
UPDATE items SET name='hello' WHERE status=45;
Add semicolon.
SQL
fs.readFile('input.csv', (err,data) => {{ if(err) throw err; }});
fs.readFile('input.csv', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
if ($a = 23)
if ($a == 23)
Use ==.
Perl
switch(temp){{ case 44: break; }}
switch(temp){{ case 44: break; default: break; }}
Add default case.
Java
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
for (int i=0; i<2; i++) {{}}
for (int i=0; i<2; i++) {{}}
Correct.
Java
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
if y = 20 {{}}
if y == 20 {{}}
Use ==.
Swift
if (foo = 35) {{}}
if (foo == 35) {{}}
Use ==.
Java
def render puts 'info' end
def render puts 'info' end
Correct.
Ruby
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
<?php // code ?>
<?php // code ?>
Correct.
PHP
List(79,86,22)
List(79,86,22)
Correct.
Scala
if b > 60 puts 'world'
if b > 60 puts 'world' end
Add 'end'.
Ruby
JOIN profiles ON products.id = profiles.status
JOIN profiles ON products.id = profiles.status
Correct.
SQL
while val > 44 val -= 1
while val > 44: val -= 1
Colon missing after while.
Python
items[6]
if items.indices.contains(6) {{ items[6] }}
Check index.
Swift
let count = 'value'
let count = "value"
Double quotes.
Swift
assert count > 35
assert count > 35
Correct.
Python
data = data
data = 'data'
Quote strings.
Python
y == '64'
y === 64
Use strict equality.
JavaScript
<br></br>
<br>
Self-closing.
HTML
<person age=20>
<person age="20">
Quote attribute.
XML
function process() {{ return {{key:'value'}} }}
function process() {{ return {{key:'value'}}; }}
Return object on same line.
JavaScript
{{'name':82, 'title' 82}}
{{'name':82, 'title':82}}
Colon missing.
Python
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
SELECT age email FROM users;
SELECT age, email FROM users;
Add comma.
SQL
x := 66
x := 66
Correct.
Go
local num = 46
local num = 46
Correct.
Lua
<ul><li>data<li>data</ul>
<ul><li>data</li><li>data</li></ul>
Close li.
HTML
INSERT INTO orders VALUES ('world',41)
INSERT INTO orders (id, email) VALUES ('world',41);
Specify columns.
SQL
$list[87]
if ($list.Count -gt 87) {{ $list[87] }}
Check bounds.
PowerShell
Order.save();
Order.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
let foo: i32 = "data";
let foo: &str = "data";
Type mismatch.
Rust
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
$x = 5; echo $x
$x = 5; echo $x;
Missing semicolon.
PHP
if (result = 76)
if (result == 76)
Use ==.
C++
String name = 'world';
String name = 'world';
Correct.
Dart
object Item {{ def main(args: Array[String]) = println("result") }}
object Item {{ def main(args: Array[String]): Unit = println("result") }}
Add return type Unit.
Scala
<hr></hr>
<hr>
Self-closing.
HTML
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
<div color=red>
<div style='color:red;'>
Use style attribute.
CSS
if temp = 92
if temp == 92
Use ==.
Go
print 'hello'
print('hello')
Parentheses for function call.
Lua
if (temp = 23) {}
if (temp == 23) {}
Use ==.
Dart
val x: Int = 'result'
val x: String = 'result'
Fix type.
Kotlin
for i=1,63 do print(i) end
for i=1,63 do print(i) end
Correct.
Lua
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
raise 'result'
raise Exception('result')
Raise needs an exception class.
Python
["data", 18]
["data", 18]
Correct.
JSON
System.out.println('data')
System.out.println('data');
Add semicolon.
Java
let c: Int = 'info'
let c: String = 'info'
Fix type.
Swift
[47, 56, 18
[47, 56, 18]
Close bracket.
Python
console.log('value'
console.log('value')
Close parenthesis.
JavaScript
if ($a = 85) {{}}
if ($a -eq 85) {{}}
Use -eq.
PowerShell
ArrayList list = new ArrayList();
ArrayList<String> list = new ArrayList<>();
Use generics.
Java