Ruby Exception Handling MCQ

This section focuses on "Exception Handling" MCQs in Ruby. These Multiple Choice Questions (mcq) should be practiced to improve the Ruby skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive examinations.

1. In ruby, Exception handling is a process which describes a way to handle the __________.

A. unwanted Event
B. unexpected event
C. Both A and B
D. None of the above

View Answer


2. Which statement is used to execute the rescue block again from the beginning after capturing the exception?

A. retry
B. raise
C. ensure
D. else

View Answer


3. What will be output for the following code?

begin
         
    raise 'Exception Created!'
   
    puts 'After Exception'
   
  rescue    
    puts 'Finally Saved!'
    
retry
end    

A. Finally Saved!
B. After Exception
C. Exception Created!
D. Infinite Loop

View Answer


4. Raise Statement is used for?

A. statement is used to raise an exception
B. statement is used to solve an exception
C. statement is used to raise and solve an exception
D. None of the above

View Answer


5. What will be output for the following code?

begin
         
    puts 'This is Before Exception Arise!'
         
       raise 'Exception Created!'
   
    puts 'After Exception' 
end

A. This is Before Exception Arise! After Exception!
B. Exception Created! After Exception!
C. This is Before Exception Arise! Exception Created!
D. This is Before Exception Arise! Exception Created! After exception

View Answer


6. Which statement will execute at the end of the code, whether the exception raise or not?

A. retry
B. raise
C. ensure
D. else

View Answer


7. What will be output for the following code?

  begin
         
    raise 'Exception Created!'
   
    puts 'After Exception' 
   
  rescue    
    puts 'Finally Saved!'
    
ensure
   puts 'ensure block execute'
end    

A. ensure block execute
B. After exception! Finally Saved! ensure block execute
C. Ecxception Created! Finally Saved! ensure block execute
D. Finally Saved! ensure block execute

View Answer


8. Which block only executes when no exception is raised?

A. retry
B. raise
C. ensure
D. else

View Answer


9. What will be output for the following code?

 begin
         
    puts 'no Exception raise' 
  
    rescue    
        puts 'Finally Saved!'
  
   else
        puts 'Else block execute because of no exception raise'
       
   ensure
      puts 'ensure block execute'
end

A. no Exception raise Else block execute because of no exception raise ensure block execute
B. no Exception raise Else block execute because of no exception raise
C. Finally Saved! Else block execute because of no exception raise ensure block execute
D. None of the above

View Answer


10. What will be output for the following code When input number is ""!""?

def catch_and_throw(value) 
  
  puts value 
  a = readline.chomp 
  
  throw :value_e if a == ""!""
  return a 
  
end
  
catch :value_e do
  
  number = catch_and_throw(""Enter Number: "") 
end

A. Error
B. 0
C. Nil
D. Infinite Loop

View Answer





Discussion


* You must be logged in to add comment.