About Programming MCQs
Programming is the cornerstone of computer science and IT. MCQs on this subject test your understanding of syntax, logic, data structures, and algorithms across various programming languages. Whether you are preparing for a university exam or a technical job interview, these questions will sharpen your problem-solving skills.
Topics Covered in This Category
- Language Fundamentals: C, C++, Java, Python, and PHP basics.
- Data Structures: Arrays, linked lists, stacks, queues, trees, and graphs.
- Algorithms: Sorting, searching, recursion, and time complexity.
- Object-Oriented Programming (OOP): Classes, objects, inheritance, polymorphism, and encapsulation.
Study Tips for Programming MCQs
Programming is best learned by doing. Do not just read the code; type it out and run it. Trace algorithms on paper to understand how variables change. When solving MCQs, pay attention to subtle syntax errors, loop conditions, and array indices, as these are common traps in exams.
Question 51: Which of these operators is used for string concatenation?
- A -
- B *
- C +
- D /
Explanation: The correct answer to the question is: +
Question 52: In Ruby, what is the default file extension for Ruby files?
- A .ru
- B .rby
- C .rb
- D .ruby
Explanation: The correct answer to the question is: .rb
Question 53: What does the **= assignment operator do?
- A It is used as exponent like 2**3=8
- B It is the multiplication operator.
- C Multiplies the value twice
- D None of the mentioned
Explanation: The correct answer to the question is: It is used as exponent like 2**3=8
Question 54: How do you break out of a loop in Ruby?
- A end()
- B exit()
- C break()
- D continue()
Explanation: The correct answer to the question is: break()
Question 55: first_name,Last_name=gets.chomp,gets.chomp is the correct way to get the input from the user.
- A True
- B False
Explanation: The correct answer to the question is: True
Question 56: What is the correct way to declare a variable in Ruby without assigning a value?
- A var_name
- B var_name = nil
- C var_name = ""
- D None of the above
Explanation: The correct answer to the question is: var_name
Question 57: What is the correct syntax for creating a hash in Ruby?
- A Hash.new("key", "value")
- B {"key1": "value1", "key2": "value2"}
- C hash = {}
- D hash.add("key", "value")
Explanation: The correct answer to the question is: {"key1": "value1", "key2": "value2"}
Question 58: Which of the following is a valid assignment operator?
- A -=
- B +=
- C All of the mentioned
- D *=
Explanation: The correct answer to the question is: All of the mentioned
Question 59: Which of the following is the correct way to define a constant in Ruby?
- A CONSTANT_NAME = value
- B constantName(value)
- C constant_name : value
- D constant_name = value
Explanation: The correct answer to the question is: CONSTANT_NAME = value
Question 60: Which of the following languages syntax matches with the Ruby's syntax?
- A Perl
- B Java
- C PHP
- D Jquery
Explanation: The correct answer to the question is: Perl
Question 61: What is the correct way to declare and initialize an array in Ruby?
- A array_name = []
- B array array_name = []
- C array_name = array[]
- D array = array_name[]
Explanation: The correct answer to the question is: array_name = []
Question 62: How do you access a value from a hash in Ruby?
- A hash.get("key")
- B hash["key"]
- C hash.at("key")
- D hash.find("key")
Explanation: The correct answer to the question is: hash["key"]
Question 63: How do you add two numbers in Ruby?
- A add(a, b)
- B a + b
- C a.add(b)
- D b.add(a)
Explanation: The correct answer to the question is: a + b
Question 64: Which symbol is used for the modulo operation in Ruby?
- A &
- B /
- C %
- D *
Explanation: The correct answer to the question is: %
Question 65: How do you pass arguments to a method in Ruby?
- A method_name.with(arg1, arg2)
- B method_name arg1 arg2
- C method_name { arg1, arg2 }
- D method_name(arg1, arg2)
Explanation: The correct answer to the question is: method_name(arg1, arg2)
Question 66: What is the correct way to declare and initialize a variable in Ruby?
- A variable_name = value
- B var_name = value
- C var var_name = value
- D variable var_name = value
Explanation: The correct answer to the question is: variable_name = value
Question 67: Which of the following is the correct way to declare a constant in Ruby?
- A constant_name : value
- B constant_name = value
- C constantName(value)
- D CONSTANT_NAME = value
Explanation: The correct answer to the question is: CONSTANT_NAME = value
Question 68: What will we the output of the given code?
- A unterminated string meets end of file
- B i'AM lEARNING ruby lANGUAGE
- C "i'am learning ruby language"
- D iam learning ruby language
Explanation: The correct answer to the question is: unterminated string meets end of file
Question 69: How do you compare two objects in Ruby?
- A object1.equals(object2)
- B object1 === object2
- C compare(object1, object2)
- D object1 == object2
Explanation: The correct answer to the question is: object1 == object2
Question 70: What is the correct way to define a conditional statement in Ruby?
- A when condition; code
- B if (condition) { code }
- C if condition then code end
- D if condition: code
Explanation: The correct answer to the question is: if (condition) { code }
Question 71: Which of the following is the correct way to define a range in Ruby?
- A (1..10)
- B [1 to 10]
- C {1..10}
- D <1,10>
Explanation: The correct answer to the question is: (1..10)
Question 72: What will be the output of the following?
- A ybuR
- B YBUR
- C RUBY
- D YBUr
Explanation: The correct answer to the question is: YBUR
Question 73: What is the result of the expression: 6 / 2 in Ruby?
- A 2.5
- B 2
- C 12
- D 3
Explanation: The correct answer to the question is: 2
Question 74: What is the correct way to define an array in Ruby?
- A array = ["element1", "element2"]
- B array = list("element1", "element2")
- C array = Array.new
- D array = new Array
Explanation: The correct answer to the question is: array = ["element1", "element2"]
Question 75: We use semicolon or parentheses after every print or puts statement.
- A True
- B False
Explanation: The correct answer to the question is: False
Question 76: What does the .upcase method do?
- A None o the mentioned
- B Convert the string to uppercase
- C Convert only lowercase string to uppercase and vice-versa
- D Convert the string to lowercase
Explanation: The correct answer to the question is: Convert the string to uppercase
Question 77: What is the method used to convert a string to lowercase in Ruby?
- A .to_lowercase
- B .to_lower
- C .lower
- D .downcase
Explanation: The correct answer to the question is: .downcase
Question 78: What is the output of the expression: "5" + "2" in Ruby?
- A 52
- B 7
- C Error
- D "7"
Explanation: The correct answer to the question is: 52
Question 79: Which of the following is the correct way to declare a variable in Ruby with a default value?
- A var_name = value
- B var_name << value
- C var_name := value
- D var_name => value
Explanation: The correct answer to the question is: var_name = value
Question 80: Which of the following are used for comparison?
- A All of the mentioned
- B Not equal to
- C Equal to
- D Less than or greater than
Explanation: The correct answer to the question is: All of the mentioned
Question 81: Which of the following is supported by Ruby?
- A Automatic Memory Management
- B All of the Mentioned
- C Dynamic Type System
- D Multiple Programming Paradigms
Explanation: The correct answer to the question is: All of the Mentioned
Question 82: Which method is used to convert a string to an array of characters in Ruby?
- A .split
- B .to_chars
- C .to_array
- D .chars
Explanation: The correct answer to the question is: .chars
Question 83: Which operator is used for subtraction in Ruby?
- A -
- B *
- C +
- D /
Explanation: The correct answer to the question is: -
Question 84: In Ruby, what is the keyword used to define a loop that repeats until a condition is true?
- A loop
- B until
- C while
- D repeat
Explanation: The correct answer to the question is: until
Question 85: What is the result of the expression: 10 % 3 in Ruby?
- A 1
- B 1.33
- C 3
Explanation: The correct answer to the question is: 1
Question 86: What is the difference between nil and false?
- A nil is a string, false is a boolean
- B nil represents absence of value, false represents a boolean value
- C Both are the same
- D nil is a number, false is a boolean
Explanation: The correct answer to the question is: nil represents absence of value, false represents a boolean value
Question 87: Which symbol is used to concatenate strings in Ruby?
- A :
- B +
- C .
- D <<
Explanation: The correct answer to the question is: <<
Question 88: What is the default return value of a method in Ruby?
- A TRUE
- B The value of the last expression
- C nil
- D FALSE
Explanation: The correct answer to the question is: nil
Question 89: Ruby does not support ++ operator, it only supports += operator.
- A False
- B True
Explanation: The correct answer to the question is: True
Question 90: Space between 2+5 or 2 + 5 is valid but =begin and = begin is not valid.
- A True
- B False
Explanation: The correct answer to the question is: True
Question 91: Methods should not be written inside double quotes.
- A False
- B True
Explanation: The correct answer to the question is: True
Question 92: What is the sequence of ruby strings?
- A None of the mentioned
- B 8-bit bytes
- C 16-bit bytes
- D 10-bit bytes
Explanation: The correct answer to the question is: 8-bit bytes
Question 93: What does the 'puts' method do in Ruby?
- A Finds the length
- B Reads input from user
- C Converts to string
- D Prints to console
Explanation: The correct answer to the question is: Prints to console
Question 94: What is the difference between =~ and == operators?
- A None of the above
- B Both operators are the same
- C =~ checks for equality, == checks for string pattern matching
- D =~ checks for string pattern matching, == checks for equality
Explanation: The correct answer to the question is: =~ checks for string pattern matching, == checks for equality
Question 95: In Ruby, what is the keyword used to define a method?
- A method
- B define
- C def
- D function
Explanation: The correct answer to the question is: def
Question 96: How to comment a single line?
- A None of the mentioned
- B Using begin and end
- C Using //
- D Using #
Explanation: The correct answer to the question is: Using #
Question 97: It is must for Ruby to use a compiler.
- A True
- B False
Explanation: The correct answer to the question is: False
Question 98: Which method is used to find the length of an array in Ruby?
- A .length_of
- B .count
- C .size
- D .length
Explanation: The correct answer to the question is: .length
Question 99: What symbol is used to denote comments in Ruby?
- A --
- B /* */
- C #
- D //
Explanation: The correct answer to the question is: #
Question 100: The .length method is used to check number of characters.
- A True
- B False
Explanation: The correct answer to the question is: False