CodeHS Programming Language Accreditations 1 — Questions and Answers
Question 1: Which of the following is the correct syntax for defining a function in Python?
- function myFunction() { }
- def myFunction(): (Correct answer)
- create myFunction() {}
- function: myFunction() { }
Correct answer: def myFunction():
In Python, functions are defined using the `def` keyword, followed by the function name, parentheses `()`, and a colon `:`. The code block belonging to the function is then indented below this definition line. This syntax clearly distinguishes function definitions from other code structures and is fundamental to Python programming.
Question 2: What is the primary purpose of the 'var' keyword in JavaScript?
- To declare a variable with a global scope
- To declare a variable with a function scope (Correct answer)
- To declare a constant variable
- To assign a value to a variable
Correct answer: To declare a variable with a function scope
In JavaScript, the `var` keyword is used to declare variables that are scoped to the function in which they are declared. This means the variable is accessible anywhere within that function, but not outside of it. While `var` can also create global variables if declared outside any function, its primary characteristic is function-level scoping, unlike `let` and `const` which are block-scoped.
Question 3: Which data type is used for storing text in Java?
- String (Correct answer)
- Char
- Text
- Str
Correct answer: String
In Java, the `String` class is specifically designed to store sequences of characters, which represent text. Unlike `char`, which holds a single character, `String` objects are immutable and can hold any length of text. It is one of the most commonly used data types for handling textual data in Java applications.
Question 4: What does the ++ operator do in C++?
- Increments the value of a variable by 1 (Correct answer)
- Decrements the value of a variable by 1
- Doubles the value of a variable
- Resets the value of a variable to zero
Correct answer: Increments the value of a variable by 1
The `++` operator in C++ is the increment operator. It is a unary operator that increases the value of its operand by one. For example, if `x` is 5, then `x++` or `++x` will change the value of `x` to 6. This operator is commonly used in loops and for simple counting operations.
Question 5: Which of the following is true about the Python keyword 'self'?
- It is used to define a global variable
- It refers to the instance of the class in object-oriented programming (Correct answer)
- It is used to define a class in Python
- It is a built-in function in Python
Correct answer: It refers to the instance of the class in object-oriented programming
In Python, `self` is a convention used as the first parameter in instance methods within a class. It explicitly refers to the instance of the class on which the method is being called, allowing access to the instance's attributes and other methods. While not a keyword in the strictest sense, it is a crucial and universally adopted identifier for referring to the object itself within its own methods.
Which of the following is the correct syntax for defining a function in Python?