programming
beginner
10 sample questions
Basic Syntax Rules MCQ Practice Test
Learning the fundamentals of syntax, including semicolons, commas, and proper indentation.
Q1. In Python, what will be the output of the following code snippet?
python
print(len(str(12345) + "hello"))
-
A. 9 ✓
-
B. 10
-
C. 8
-
D. Error
Explanation: The code first converts the integer 12345 to a string. Then, it concatenates this string with the string "hello". Finally, it calculates the length of the resulting string, which is "12345hello", having a length of 9.
Q2. In the Python programming language, which of the following statements correctly assigns the string value "Hello, world!" to the variable \"greeting\".
-
A. greeting = "Hello, world!" ✓
-
B. greeting := "Hello, world!"
-
C. "Hello, world!" = greeting
-
D. greeting == "Hello, world!"
Explanation: In Python, the assignment operator '=' is used to assign a value to a variable. The other options use incorrect syntax for assignment.
Q3. In the Python programming language, which of the following code snippets will correctly print the string "Hello, world!" to the console?
-
A. print ("Hello, world!") ✓
-
B. System.out.println("Hello, world!");
-
C. Console.WriteLine("Hello, world!");
-
D. echo "Hello, world! "
Explanation: The `print()` function is the standard way to output text to the console in Python. The other options use syntax from Java, C# and bash scripting respectively.
Q4. In the Python programming language, what will be the output of the following code snippet?
python
print(str(10) + "hello")
-
A. 10hello ✓
-
B. An error occurs
-
C. 10 + hello
-
D. "10hello"
Explanation: In Python, the '+' operator performs string concatenation when used with strings. The `str(10)` converts the integer 10 to its string representation, '10'. Therefore, '10' and "hello" are concatenated, resulting in the output "10hello".
Q5. In many programming languages, what symbol is typically used to denote a single-line comment?
Explanation: In languages like C++, Java, JavaScript, and others, '//' indicates that the text following it on the same line is a comment and should be ignored by the compiler or interpreter.
Q6. In Python, what will be the output of the following code snippet?
python
print("Hello, " + str(5) + "!")
-
A. Hello, 5! ✓
-
B. Hello, 5
-
C. Error: cannot concatenate string and integer
-
D. Hello, +5!
Explanation: Python's `+` operator performs string concatenation when used with strings. The `str(5)` converts the integer 5 into a string, allowing concatenation with the other strings. Therefore, the output is 'Hello, 5!'
Q7. In many programming languages, what is the primary purpose of a semicolon (;)?
-
A. To start a new line of code
-
B. To indicate the end of a statement ✓
-
C. To separate variables in a declaration
-
D. To declare a function
Explanation: Semicolons are used to signal the compiler or interpreter that a complete instruction or statement has been written. Different languages have varying rules about whether semicolons are strictly required, but they often play this crucial role in separating independent statements.
Q8. In Python, what will be the output of the following code snippet?
python
print("Hello" + " " + str(5))
-
A. Hello 5 ✓
-
B. Hello\ 5
-
C. Error: Cannot concatenate strings and integers
-
D. Hello\u00205
Explanation: In Python, the '+' operator can concatenate strings. The `str(5)` converts the integer 5 into a string, allowing concatenation with the other strings. Therefore, the output is "Hello 5".
Q9. In many programming languages, what is the primary purpose of a semicolon ';' at the end of a line of code?
-
A. To indicate the beginning of a code block
-
B. To separate variables from their values
-
C. To signal the end of a statement ✓
-
D. To denote a comment within the code
Explanation: Semicolons are used as statement terminators in many languages like C, C++, Java, and JavaScript. They tell the compiler or interpreter where one statement ends and the next begins.
Q10. Consider a programming language where variable names must start with a letter (a-z, A-Z) and can contain letters, numbers (0-9), and underscores (_). Which of the following variable names would be considered INVALID in this language?
-
A. myVariable_1
-
B. 1stVariable ✓
-
C. _myVar
-
D. variableName
Explanation: Variable names cannot begin with a number. '1stVariable' violates this rule, while the others are valid according to the specified constraints.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free