Skip to content

Latest commit

 

History

History
664 lines (534 loc) · 38.7 KB

File metadata and controls

664 lines (534 loc) · 38.7 KB

Step 02: Introducing JShell

Question 4: What does JShell stand for?

  • A) JavaShell Correct: JShell stands for Java Shell. It's a Read-Eval-Print Loop (REPL) tool introduced in Java 9.
  • B) JavaScriptShell Incorrect: JShell is a Java feature and has no relation to JavaScript, a separate programming language.
  • C) JupyterShell Incorrect: Jupyter is a different open-source project providing interactive computing for multiple programming languages. It isn't directly connected to Java or JShell.

Question 5: What is the purpose of JShell?

  • A) To create graphical user interfaces Incorrect: GUI creation in Java typically involves libraries like Swing or JavaFX, not JShell.
  • B) To evaluate and execute Java code and provide immediate results Correct: JShell is designed to evaluate and execute Java code and provide immediate results, a feature known as a Read-Eval-Print Loop (REPL).
  • C) To debug Java code Incorrect: Although you can use JShell to test small code snippets, its primary function isn't debugging. Traditional debugging involves tools that step through code, inspect variables, set breakpoints, etc.

Question 6: What command can be used to exit JShell?

  • A) /end Incorrect: /end is not a valid command in JShell.
  • B) /exit Correct: /exit is the command to exit JShell, terminating the session and returning you to your system's command line.
  • C) /quit Incorrect: /quit is not a recognized command in JShell.

Step 03: Welcome to Problem Solving

Question 7: What is the problem solving technique used in this step?

  • A) Reverse Engineering Incorrect: Reverse Engineering starts with a finished product, working backward to understand how it was made. It doesn't apply in this context.
  • B) Subdivision Correct: Subdivision is a technique where a complex problem is broken down into smaller, more manageable parts. It's used in this step to simplify the task of creating a multiplication table.
  • C) Deductive Reasoning Incorrect: Deductive Reasoning is a process of reasoning from general statements to a certain conclusion. It's not the primary technique used in this situation.

Question 8: What is the result of 5 * 3?

  • A) 8 Incorrect: 5 times 3 equals to 15, not 8.
  • B) 10 Incorrect: 5 times 3 equals to 15, not 10.
  • C) 15 Correct: The result of multiplying 5 by 3 is indeed 15.

Question 9: How many times do we repeat the calculation in this step?

  • A) 5 times Incorrect: The calculation is not repeated 5 times in this step.
  • B) 10 times Correct: The calculation is repeated 10 times in this step.
  • C) 15 times Incorrect: The calculation is not repeated 15 times in this step.

Step 04: Introducing Expressions

Question 10: What is the result of 5 * 3?

  • A) 8 Incorrect: 5 times 3 equals to 15, not 8.
  • B) 10 Incorrect: 5 times 3 equals to 15, not 10.
  • C) 15 Correct: The result of multiplying 5 by 3 is indeed 15.

Question 11: What are the operands in the expression 5 * 3?

  • A) 5, 3 Correct: In the expression 5 * 3, 5 and 3 are the operands being multiplied.
  • B) 5 Incorrect: Both 5 and 3 are operands in the expression, not just 5.
  • C) 3 Incorrect: Both 5 and 3 are operands in the expression, not just 3.

Question 12: What is the modulo operator in Java?

  • A) * Incorrect: The asterisk (*) is the multiplication operator in Java, not the modulo operator.
  • B) % Correct: The percentage symbol (%) represents the modulo operator in Java. It returns the remainder of a division operation.
  • C) + Incorrect: The plus symbol (+) is the addition operator in Java, not the modulo operator.

Step 05: Programming Exercise PE-1 (With Solutions)

Question 13: Write an expression to calculate the number of minutes in a day.

  • A) 24 * 60 Incorrect: This is the number of hours in a day times the number of minutes in an hour, not the number of minutes in a day.
  • B) 60 + 24 Incorrect: This expression adds the number of hours in a day to the number of minutes in an hour, which doesn't give the number of minutes in a day.
  • C) 60 * 24 Correct: There are 60 minutes in an hour and 24 hours in a day. Therefore, the number of minutes in a day is 60 * 24.

Question 14: Write an expression to calculate the number of seconds in a day.

  • A) 24 * 60 * 60 Incorrect: This is the number of hours in a day times the number of minutes in an hour times the number of seconds in a minute, which doesn't yield the number of seconds in a day.
  • B) 60 + 60 + 24 Incorrect: This expression adds the number of minutes in an hour, the number of seconds in a minute, and the number of hours in a day. It doesn't calculate the number of seconds in a day.
  • C) 60 * 60 * 24 Correct: There are 60 seconds in a minute, 60 minutes in an hour, and 24 hours in a day. So, the number of seconds in a day is 60 * 60 * 24.

Step 06: Operators

Question 15: Which of the following is a valid operator in Java?

  • A) ** Incorrect: Java doesn't have a ** operator. This is used in some languages for exponentiation, but Java uses Math.pow() for this purpose.
  • B) $ Incorrect: The dollar symbol ($) is not an operator in Java.
  • C) * Correct: The asterisk (*) is the multiplication operator in Java.

Question 16: What is the result of 5 / 2 in Java?

  • A) 2 Correct: In Java, when you divide an integer by another integer, the result is an integer. So, 5 divided by 2 equals 2, with remainder 1.
  • B) 2.0 Incorrect: 5 divided by 2 yields 2.5, but since both are integers, the result is an integer (2), not a floating-point number (2.0).
  • C) 2.5 Incorrect: 5 divided by 2 equals 2.5, but in Java, when you divide an integer by another integer, the result is an integer (2), not a floating-point number (2.5).

Question 17: What is the order of sub-expression evaluation in the expression 5 + 5 * 6?

  • A) 5 + 5, then 10 * 6 Incorrect: In Java, multiplication and division are performed before addition and subtraction. Therefore, 5 * 6 is evaluated first, then 5 + 30.
  • B) 5 * 6, then 5 + 30 Correct: Java follows the order of operations, which means that multiplication and division are performed before addition and subtraction.
  • C) Depends on operand types Incorrect: The order of operations in Java doesn't depend on operand types.

Question 18: How can parentheses be used to group parts of an expression?

  • A) They have no effect on the evaluation of an expression Incorrect: Parentheses do have an effect on expression evaluation; they dictate the order in which operations are performed.
  • B) They are used to specify the order of sub-expression evaluation Correct: Parentheses can be used to change the order of operations in an expression, ensuring certain parts are evaluated first.
  • C) They are used to introduce new operators Incorrect: Parentheses don't introduce new operators; they are used to change the order of operations in an expression.

Step 07: Introducing Console Output

Question 19: Which built-in Java method displays text on the console?

  • A) System.out.print() Incorrect: While this method does display text, it does not add a new line after the text.
  • B) System.out.display() Incorrect: There's no display() method in System.out in Java.
  • C) System.out.println() Correct: This method displays text on the console and adds a new line after the text.

Question 20: What is a String literal in Java?

  • A) A sequence of numbers Incorrect: A string literal is a sequence of characters enclosed in double quotes, not necessarily numbers.
  • B) A piece of text with numeric characters Incorrect: A string literal can be any sequence of characters, not just numeric characters.
  • C) A keyword used for control flow Incorrect: A string literal isn't a keyword. It's a sequence of characters enclosed in double quotes.

Question 21: How do you print the exact text "Hello World" on the console using System.out.println()?

  • A) System.out.println("Hello World") Correct: This is the correct way to print "Hello World" in Java. The text to be printed is enclosed in double quotes.
  • B) System.out.println(Hello World) Incorrect: This will cause a compilation error, as Hello and World are not valid variables or keywords, and the text is not enclosed in double quotes.
  • C) System.out.print("Hello World") Incorrect: While this will print "Hello World", it will not add a new line afterwards, unlike System.out.println().

Question 22: Can you pass a mathematical expression like 5 * 3 = 15 as an argument to System.out.println() in Java?

  • A) Yes, it will evaluate the expression and print the result Incorrect: You can't pass a mathematical expression with an equals sign (=) to System.out.println() in Java. This will cause a syntax error.
  • B) No, it will throw an error `Correct: In Java, you can't pass a mathematical expression with an equals sign (=) as an argument to System.out.println(). You can only pass an expression without an equals

sign, such as 5 * 3, and Java will evaluate and print the result.`

  • C) Yes, it will print the expression as it is without evaluating it Incorrect: You can't pass a mathematical expression with an equals sign (=) to System.out.println() in Java. This will cause a syntax error.

Step 09: Solutions to PE-02

Question 23: What is the output of the following code?

System.out.println("Hello World");
  • A) HelloWorld Incorrect: The correct output will include a space between "Hello" and "World", because the argument to the println method is a single string with a space included.
  • B) Hello World Correct: This option correctly reproduces the argument to the println method, including the space between "Hello" and "World".
  • C) "Hello World" Incorrect: The quotation marks are not included in the output of the println method. They are used in the code to define the string that is printed.

Question 24: What is the output of the following code?

System.out.println("5 * 3");
  • A) 5 * 3 Correct: This will print the string "5 * 3" as it is without evaluating it because it's in the double quotes.
  • B) 15 Incorrect: Java won't evaluate the expression inside the quotes. It will consider it as a string.
  • C) "5 * 3" Incorrect: The output will not include the double quotes. They are used in the code to define the string that is printed.

Question 25: What is the output of the following code?

System.out.println(5 * 3);
  • A) 5 * 3 Incorrect: Java will evaluate the expression 5 * 3 as it's not inside the double quotes and print the result.
  • B) 15 Correct: This statement is doing arithmetic operation (multiplication) and the result of 5 * 3 is 15.
  • C) "15" Incorrect: The output is the integer 15, not the string "15". There are no quotation marks in the output.

Question 26: What is the output of the following code?

System.out.println(60 * 60 * 24);
  • A) 86400 Correct: This statement is doing arithmetic operation (multiplication) and the result of 60 * 60 * 24 is 86400.
  • B) 86400.0 Incorrect: The output is the integer 86400, not the floating-point number 86400.0. The multiplication operation does not involve any floating-point numbers.
  • C) "86400" Incorrect: The output is the integer 86400, not the string "86400". There are no quotation marks in the output.

Step 10: Whitespace, Case Sensitiveness and Escape Characters

Question 27: What does whitespace refer to in Java?

  • A) Any sequence of continuous digits.
    • Incorrect: Whitespace does not refer to digits in Java. It refers to spaces, tabs, and newlines.
  • B) Any sequence of continuous space, tab or newline characters.
    • Correct: In Java, "whitespace" refers to any space, tab, or newline character. These characters are often used to format code to make it more readable.
  • C) Any sequence of continuous special symbols.
    • Incorrect: Whitespace does not refer to special symbols in Java. It refers to spaces, tabs, and newlines.

Question 28: Why is it important to use the correct case when calling pre-defined Java elements?

  • A) Because Java is case-sensitive, and using the incorrect case will result in an error.
    • Correct: Java is case-sensitive. For instance, the method "println" is different from "Println" or "PRINTLN". Incorrect case can cause a compilation error.
  • B) Because Java is not case-sensitive, and using the incorrect case will make the code more readable.
    • Incorrect: Java is case-sensitive. The readability of the code depends on following coding conventions, not on the case-sensitivity of the language.
  • C) It is not important to use the correct case when calling pre-defined Java elements.
    • Incorrect: Java is a case-sensitive language, meaning that using the incorrect case will result in a compilation error.

Question 29: What does an escape character do in Java?

  • A) It helps to escape from the current method.
    • Incorrect: An escape character does not have anything to do with escaping from a method. It is used to introduce special character sequences.
  • B) It helps to insert special character sequences.
    • Correct: An escape character (\) in Java is used to introduce special character sequences like \n for newline, \t for tab, \" for double quote, etc.
  • C) It helps to escape from the current class.
    • Incorrect: An escape character does not have anything to do with escaping from a class. It is used to introduce special character sequences.

Question 30: What is the purpose of the '\n' escape sequence?

  • A) To insert a new method.
    • Incorrect: The '\n' escape sequence doesn't relate to inserting methods. It's used to insert a newline in the text at this point.
  • B) To insert a newline.
    • Correct: The '\n' escape sequence is used to insert a newline in the text at this point.
  • C) To insert a tab.
    • Incorrect: The '\n' escape sequence does not insert a tab. The '\t' escape sequence is used for that purpose.

Question 78: What is the escape sequence for inserting a tab in a string literal?

  • A) \n
    • Incorrect: The \n escape sequence represents a newline, not a tab.
  • B) \t
    • Correct: The \t escape sequence is used for inserting a tab.
  • C) \b
    • Incorrect: The \b escape sequence represents a backspace, not a tab.

Step 11: More On Method Calls

Question 31: What is the purpose of parentheses in method calls?

  • A) They are optional and can be left out
    • Incorrect: Parentheses are not optional in method calls and cannot be left out. They are part of the syntax.
  • B) They are used to separate individual parameters with a comma
    • Incorrect: While parameters are indeed separated by commas within parentheses, the primary purpose of parentheses in method calls is to enclose all the parameters.
  • C) They enclose all the parameters and are a necessary part of the syntax
    • Correct: In method calls, parentheses are used to enclose all parameters and are a necessary part of the syntax.

Question 32: What does the Math.random() method do?

  • A) It prints a random integer between 0 and 1
    • Incorrect: Math.random() returns a random double value between 0 (inclusive) and 1 (exclusive). It does not print anything.
  • B) It returns a random real number between 0 and 1
    • Correct: Math.random() generates a random double value between 0.0 (inclusive) and 1.0 (exclusive).
  • C) It returns the maximum of two given numbers
    • Incorrect: Math.random() does not compare or return maximum values. It returns a random double value.

Question 33: What does the Math.min() method return?

  • A) The maximum of two given numbers
    • Incorrect: The Math.min() method is used to find the minimum of the two numbers, not the maximum.
  • B) The minimum of two given numbers
    • Correct: The Math.min() method returns the smallest of two numbers.
  • C) The sum of two given numbers
    • Incorrect: The Math.min() method does not sum numbers. It returns the smallest of the two numbers.

Step 12: More Formatted Output

Question 34: Which method can accept a variable number of arguments?

  • A) System.out.println()
    • Incorrect: System.out.println() accepts a single argument, but it can print multiple values if they are concatenated into a single string.
  • B) System.out.printf()
    • Correct: System.out.printf() can accept a variable number of arguments, allowing for formatted output of multiple values.
  • C) Math.min()
    • Incorrect: The Math.min() method takes two arguments and returns the smaller one.

Question 35: Which of the following is a predefined literal used as a format specifier in printf() to format data of type int?

  • A) %f
    • Incorrect: The %f specifier is used for floating-point types, not integers.
  • B) %d
    • Correct: The %d specifier is used to format integers.
  • C) %s
    • Incorrect: The %s specifier is used for strings, not integers.

Question 36: What will be the output of the following code?

System.out.printf("%d + %d + %d = %d", 3, 4, 5, 3 + 4 + 5).println();
  • A) 3 + 4 + 5 = 12
    • Correct: The printf statement formats the output according to the format specifiers and arguments provided. Hence, the output will be '3 + 4 + 5 = 12'.
  • B) 3 4 5 = 12
    • `Incorrect: The

output includes plus signs due to the format string in the printf method.`

  • C) 3 + 4 = 7
    • Incorrect: The output includes 3 numbers and their sum, which is 12, not just two numbers and their sum.

Step 13: Introducing Variables

Question 37: What is a variable in Java?

  • A) A method to print calculated values
    • Incorrect: A variable is not a method. It is used to store data, not print values.
  • B) A keyword to reserve memory for data
    • Correct: A variable in Java is a location in memory where data can be stored. When we declare a variable, we reserve memory for data.
  • C) A character to represent symbols in a string
    • Incorrect: A variable is not a character or a symbol. It is a memory location for storing data values.

Question 38: How can we change the value of a variable?

  • A) Using the System.out.printf() method
    • Incorrect: The System.out.printf() method is used for formatted output, not for changing variable values.
  • B) By calling a method with a different argument
    • Incorrect: While it's true that a method can change a variable's value, it's not the main or direct way to do so.
  • C) By assigning a new value to it using the assignment operator =
    • Correct: We can change the value of a variable by assigning a new value to it using the assignment operator (=).

Question 39: How can we use variables to simplify code?

  • A) By defining a new method
    • Incorrect: Defining a new method does not directly relate to using variables. Variables can help to store and manipulate data within methods, but creating new methods is not a direct way of using variables to simplify code.
  • B) By storing the output of a method in a variable
    • Incorrect: While this is a use of variables, the most general way variables simplify code is by holding data that can be used and manipulated in many ways, including within different parts of a statement or block of code.
  • C) By assigning changing values to a variable and using it in a statement
    • Correct: Variables can be used to simplify code by storing changing values, which can then be referred to and manipulated in various parts of a program.

Step 15: Using Variables Quiz

Question 40: Why is it important to declare variables before using them in a Java program?

  • A) It makes the code look more organized
    • Incorrect: While declaring variables does help to organize code, the main reason to declare them before use is to ensure they are initialized before being used.
  • B) It ensures that the variables have values assigned to them
    • Incorrect: While it's true that declaring variables can ensure they have initial values, the main reason to declare them before use is to avoid runtime errors due to uninitialized variables.
  • C) It avoids errors due to uninitialized variables
    • Correct: Declaring variables before using them avoids runtime errors because it ensures that memory is allocated for them and they are initialized to default or user-specified values.

Question 41: In Java, every variable must be declared with a __________.

  • A) keyword
    • Incorrect: Although a keyword is used to indicate the data type during variable declaration (like int, double, etc.), it is not what is declared with the variable itself.
  • B) value
    • `Incorrect: While a variable can be declared with an initial value,

it is not a requirement.`

  • C) type
    • Correct: In Java, every variable must be declared with a data type. The type specifies the kind of values the variable can store and the operations that can be performed on it.

Question 42: What happens when you try to store a double value into an integer variable?

  • A) It is allowed without any issues
    • Incorrect: Java does not automatically convert a double value into an integer because it could result in loss of precision.
  • B) An error is generated as the types are incompatible
    • Correct: If you try to store a double value into an integer variable, Java generates a compile-time error because of the incompatibility in types.
  • C) The value is automatically rounded up to the nearest integer
    • Incorrect: Java does not automatically round the double value. Explicit casting is required for this.

Step 16: Variables: Behind-The-Scenes

Question 43: What is the process of giving a value to a variable during its declaration called?

  • A) Assignment
    • Incorrect: While assigning a value to a variable is part of the process, the term for giving a value to a variable during its declaration is "initialization".
  • B) Initialization
    • Correct: Initialization is the process of giving a value to a variable at the time of declaration.
  • C) Declaration
    • Incorrect: Declaration is the process of creating a variable, not the act of giving it a value.

Question 44: What happens when a variable's initial value is another variable previously defined?

  • A) The initial value of the first variable is lost
    • Incorrect: Assigning the value of one variable to another does not affect the original variable's value.
  • B) The value of the second variable is lost
    • Incorrect: The value of the second variable is updated, not lost, when it's given the value of the first variable.
  • C) The value stored in the first variable's slot is copied to the second variable's slot
    • Correct: When one variable is assigned the value of another, the value in the first variable is copied into the second.

Question 45: Which of the following is not allowed in variable assignment?

  • A) From a literal value to a variable, having compatible types
    • Incorrect: This is allowed. A literal value of a compatible type can be assigned to a variable.
  • B) From a variable to another variable, of compatible types
    • Incorrect: This is allowed. The value of one variable can be assigned to another, provided their types are compatible.
  • C) Assignment to a constant literal
    • Correct: This is not allowed. Constant literals cannot be assigned new values.

Step 17: Naming Variables

Question 46: Which of the following characters is allowed in a variable name in Java?

  • A) #
    • Incorrect: The # character is not allowed in a variable name in Java.
  • B) $
    • Correct: The $ character is allowed in a variable name in Java.
  • C) %
    • Incorrect: The % character is not allowed in a variable name in Java.
  • D) !
    • Incorrect: The ! character is not allowed in a variable name in Java.

Question 47: Which of the following variable names is not allowed in Java?

  • A) _score

    • Incorrect: This is allowed. A variable name can start with an underscore (_) in Java.
  • B) 3goals

    • Correct: This is not allowed. A variable name cannot start with a number in Java.
  • C) player$1

    • Incorrect: This is allowed. A variable name can include $ in Java.

Question 48: What is the purpose of using meaningful names for variables in a program?

  • A) To make the program run faster
    • Incorrect: The names of variables do not affect the speed of a program.
  • B) To make the code easier to read and understand
    • Correct: Using meaningful names for variables makes the code easier to read and understand, as it provides some indication of what the variables are used for.
  • C) To use less memory
    • Incorrect: The names of variables do not significantly impact the amount of memory used by a program.

That's the end of the quiz. I hope this was helpful for your learning process.

Question 49: Which of the following characters is allowed in a variable name in Java?

  • A) #
    • Incorrect: The # character is not allowed in a variable name in Java.
  • B) $
    • Correct: The $ character is allowed in a variable name in Java.
  • C) %
    • Incorrect: The % character is not allowed in a variable name in Java.
  • D) !
    • Incorrect: The ! character is not allowed in a variable name in Java.

Question 50: Which of the following variable names is not allowed in Java?

  • A) _score
    • Incorrect: This is allowed. A variable name can start with an underscore (_) in Java.
  • B) 3goals
    • Correct: This is not allowed. A variable name cannot start with a number in Java.
  • C) yellowCard
    • Incorrect: This is allowed. This follows the standard naming convention in Java.
  • D) goals3
    • Incorrect: This is allowed. A variable name can have numbers, but it cannot start with them.

Question 51: Which of the following is a recommended naming convention for variables in Java?

  • A) Start variable names with uppercase letters
    • Incorrect: In Java, it is common convention to start variable names with lowercase letters.
  • B) Use long variable names to make your code more expressive
    • Incorrect: While clarity is important, excessively long variable names can make code harder to read. It's best to keep them concise yet meaningful.
  • C) Use CamelCase for variable names with multiple words
    • Correct: In Java, it is common convention to use CamelCase for variable names that consist of multiple words.
  • D) Use special characters like ! or % in variable names
    • Incorrect: Java does not allow special characters like ! or % in variable names.

Question 52: Which of the following is NOT an integral value primitive type in Java?

  • A) byte
    • Incorrect: byte is an integral value primitive type in Java.
  • B) short
    • Incorrect: short is an integral value primitive type in Java.
  • C) float
    • Correct: float is a floating-point primitive type, not an integral type.

Question 53: What is the default type for floating type values in Java with size 64 bits?

  • A) double
    • Correct: double is the default type for floating-point numbers in Java with a size of 64 bits.
  • B) float
    • Incorrect: float is a 32-bit floating point type.
  • C) int
    • Incorrect: int is an integer type, not a floating-point type.

Question 54: What is the correct way to store a single character symbol in a char variable in Java?

  • A) Within double quotes ""
    • Incorrect: Double quotes are used to denote Strings in Java. For character literals, single quotes are used.
  • B) Within parentheses ()
    • Incorrect: Parentheses are not used to denote character literals in Java.
  • C) Within single quotes ''
    • Correct: Single quotes are used to denote character literals in Java.

Question 55: Which data type is best for storing the number of goals scored by a team in a football match?

  • A) Byte

    • Incorrect: While byte could technically store the number of goals in a single match, it is rarely used for this purpose due to its limited range.
  • B) Short

    • Correct: The short data type is a good choice for storing the number of goals in a football match, as it can comfortably accommodate the range of possible values.
  • C) Long

    • Incorrect: The long data type is more than is needed for storing the number of goals in a football match. It would be wasteful in terms of memory.

Question 56: Which data type is best for storing the average rainfall in a month?

  • A) Int
    • Incorrect: Integers are not ideal for storing average rainfall, as rainfall is often measured to a decimal point.
  • B) Float
    • Incorrect: Although float could work, it has less precision compared to double.
  • C) Double
    • Correct: The double data type is ideal for storing the average rainfall in a month because it can comfortably accommodate decimal values to a high degree of precision.

Question 57: Which data type is best for storing the grade of a student in a class?

  • A) Int
    • Incorrect: Integers are not ideal for storing grades, especially if the grading system involves letters (A, B, C, etc.) or decimal points.
  • B) Char
    • Correct: The char data type is ideal for storing the grade of a student if the grades are represented by single letters (A, B, C, etc.).
  • C) Float
    • Incorrect: Float would be suitable if grades were numerical and required decimal precision, but for letter grades, char is more appropriate.

Question 58: What is the assignment operator in Java?

  • A) ==
    • Incorrect: == is the equality operator in Java, not the assignment operator.
  • B) =
    • Correct: = is the assignment operator in Java.
  • C) +
    • Incorrect: + is the addition operator in Java, not the assignment operator.

Question 59: What is the output of the following code: int x = 5; x = x + 3;

  • A) 3
    • Incorrect: The variable x is incremented by 3, so its new value is 8, not 3.
  • B) 8
    • Correct: The variable x is incremented by 3, giving it a new value of 8.
  • C) 5
    • Incorrect: The variable x is incremented by 3, so its new value is 8, not 5.

Question 60: What is the result of the following code: int y = 10; y = y - 2; y = y - 3;

  • A) 3
    • Incorrect: The variable y is decremented by 2 and then by 3, resulting in a value of 5, not 3.
  • B) 10
    • Incorrect: The variable y is decremented by 2 and then by 3, resulting in a value of 5, not 10.
  • C) 5
    • Correct: The variable y is decremented by 2 and then by 3, resulting in a value of 5.

Question 61: What is the difference between prefix and postfix versions of increment and decrement operators?

  • A) There is no difference between prefix and postfix versions.
    • Incorrect: There is indeed a difference: the prefix version updates the variable first and then returns the value, while the postfix version first returns the value and then updates it.
  • B) Prefix version returns the updated value while postfix returns the original value.

Correct: The prefix version increments or decrements the variable first and then returns the value, while the postfix version first returns the value and then increments or decrements it.

  • C) Postfix version returns the updated value while prefix returns the original value.
    • Incorrect: This is the opposite of how prefix and postfix operators work in Java.

Question 62: What is the compound assignment operator used for?

  • A) To assign a new value to a variable.
    • Incorrect: Although compound assignment does involve assigning a value to a variable, its main purpose is to simplify the syntax when the variable itself is part of the operation.
  • B) To combine the = with a numeric operator.
    • Correct: Compound assignment operators combine the assignment operator (=) with a numeric operator such as +, -, *, or /.
  • C) To compare two values.
    • Incorrect: Compound assignment operators are not used for comparison.

Question 63: What does the expression i %= 2 mean?

  • A) Add 2 to the value of i and store the result back into i.
    • Incorrect: This would be represented by the expression i += 2, not i %= 2.
  • B) Divide i by 2, and store the result back into i.
    • Incorrect: This would be represented by the expression i /= 2, not i %= 2.
  • C) Divide i by 2, and store the remainder back into i.
    • Correct: The %= operator performs modulus division, dividing i by 2 and storing the remainder back into i.

Question 64: Which is the correct comparison operator in Java?

  • A) =
    • Incorrect: = is the assignment operator in Java, not the comparison operator.
  • B) ==
    • Correct: == is the comparison operator in Java, which checks if two variables are equal.

Question 65: What is the structure of an if statement in Java?

  • A) if {condition} (statement);
    • Incorrect: Brackets are incorrectly placed in this statement. The condition should be inside parentheses and the statement inside braces.
  • B) if {statement} (condition);
    • Incorrect: Brackets are incorrectly placed in this statement. The condition should be inside parentheses and the statement inside braces.
  • C) if (condition) {statement};
    • Correct: This is the correct syntax for an if statement in Java.

Question 66: What is the output of the following code:

int a = 5;
int b = 10;
if (a > b) {
  System.out.println("a is greater than b");
} else {
  System.out.println("b is greater than a");
}
  • A) a is greater than b
    • Incorrect: The condition checks if a is greater than b, which is false as 5 is not greater than 10.
  • B) b is greater than a
    • Correct: Since a is not greater than b, the else statement is executed, printing "b is greater than a".

Question 67: What is the output of the following code snippet?

int x = 7;
if (x < 10) {
    System.out.println("x is less than 10");
}
else {
    System.out.println("x is greater than or equal to 10");
}
  • A) x is less than 10
    • Correct: The variable x is less than 10, so this statement is printed.
  • B) x is greater than or equal to 10
    • Incorrect: The variable x is less than 10, so this statement is not printed.
  • C) Compiler Error
    • Incorrect: There is no compiler error. The code is correct.

Question 68: What happens when we don't use statement blocks with if conditional?

  • A) The condition statement only controls the execution of the first statement.
    • Correct: If no braces are used, an if condition only applies to the immediate statement following it.
  • B) The condition statement controls the execution of all the statements.
    • Incorrect: Without braces, only the immediate following statement is controlled by the if condition.
  • C) The code will not compile.
    • Incorrect: The code will compile. It's just that the if condition will only apply to the immediate next statement.

Question 69: What is the benefit of using statement blocks with if conditionals?

  • A) Improves code readability
    • Correct: Using braces makes it clear which statements are controlled by the if condition, improving readability.
  • B) Increases the number of statements that can be executed conditionally
    • Incorrect: While using braces does allow multiple statements to be executed under a condition, the primary benefit is improving readability, not increasing functionality.
  • C) Improves program efficiency
    • Incorrect: Using braces does not improve program efficiency. Its primary benefit is improving readability.

Question 70: What does a for loop iterate over?

  • A) a fixed number of iterations
    • `Incorrect: A for loop can iterate over a

variable number of iterations, not just a fixed number.`

  • B) an infinite number of times
    • Incorrect: A for loop can run an infinite number of times only if its condition never becomes false, but generally it iterates over a variable number of times.
  • C) a variable number of times
    • Correct: A for loop can iterate over a variable number of times, depending on its condition.

Question 71: What is the syntax of a for loop?

  • A) for (initialization; update; condition)
    • Incorrect: This is not the correct syntax for a for loop in Java. The correct order is: initialization; condition; update.
  • B) for (update; condition; initialization)
    • Incorrect: This is not the correct syntax for a for loop in Java. The correct order is: initialization; condition; update.
  • C) for (initialization; condition; update)
    • Correct: This is the correct syntax for a for loop in Java.

Question 72: What is the output of the following code snippet?

for (int i=0; i<=10; i++) {
    System.out.printf("%d * %d = %d", 6, i, 6*i).println();
}
  • A) Multiplication table of 6
    • Correct: This code prints the multiplication table of 6.
  • B) Multiplication table of 10
    • Incorrect: The loop is multiplying by 6, not 10.
  • C) Multiplication table of 7
    • Incorrect: The loop is multiplying by 6, not 7.

Question 73: What is the output of the following code snippet?

for (int i=1; i<=10; i++) {
    System.out.printf(i*i).println();
}
  • A) The squares of integers from 1 to 10
    • Correct: The code calculates and prints the squares of the integers from 1 to 10.
  • B) The squares of first 10 even integers
    • Incorrect: The code calculates the squares of all integers from 1 to 10, not just the even ones.
  • C) The squares of first 10 odd integers
    • Incorrect: The code calculates the squares of all integers from 1 to 10, not just the odd ones.

Question 74: What is the output of the following code snippet?

for (int i=10; i>0; i--) {
    System.out.printf(i).println();
}
  • A) The integers from 10 to 1
    • Correct: The code prints the integers from 10 to 1 in descending order.
  • B) The integers from 1 to 10
    • Incorrect: The loop is set to decrement, not increment, so it prints from 10 to 1, not 1 to 10.
  • C) The squares of integers from 1 to 10 in reverse order
    • Incorrect: The loop simply prints the numbers from 10 to 1, it does not square the numbers.

Question 75: Which of the following is true about the for loop construct in Java?

  • A) All components of the for loop are required.
    • Incorrect: All components of the for loop (initialization, condition, and update) are optional in Java.
  • B) Only the condition component of the for loop is optional.
    • Incorrect: All components of the for loop are optional in Java, not just the condition.
  • C) All components of the for loop are optional.
    • Correct: In a Java for loop, all components are optional.

Question 76: What happens if the condition component of a for loop is left empty?

  • A) The loop continues indefinitely until stopped externally.
    • `Correct: If the condition component of a for loop is left empty, the

loop will continue indefinitely (or until an external condition, like a break statement, stops it).`

  • B) The loop exits immediately without executing any statements.
    • Incorrect: The loop will continue to execute, not exit, if the condition is left empty.
  • C) The loop executes once and exits immediately.
    • Incorrect: The loop will continue to execute indefinitely, not just once, if the condition is left empty.

Question 77: What is the output of the following code snippet?

int i = 1;
for (; i <= 10; i++);
System.out.println(i);
  • A) 1
    • Incorrect: The variable i is incremented in the for loop until it is not less than or equal to 10, so the output will be 11.
  • B) 10
    • Incorrect: The variable i is incremented in the for loop until it is not less than or equal to 10, so the output will be 11.
  • C) 11
    • Correct: The variable i is incremented in the for loop until it is not less than or equal to 10, so the output will be 11.