• +91 9723535972
  • info@interviewmaterial.com

Ruby Interview Questions and Answers

Ruby Interview Questions and Answers

Question - 61 : - What is yield statement in Ruby.

Answer - 61 : -

The yield statement is used to call a block within a method with a value.

Question - 62 : - What are freezing string in Ruby.

Answer - 62 : -

In most programming languages strings are immutable. It means that an existing string can't be modified, only a new string can be created out of them.

In Ruby, by default strings are not immutable. To make them immutable, freeze method can be used.

Question - 63 : - In how many ways you can compare Ruby string?

Answer - 63 : -

Ruby strings can be compared with three operators:

  • With == operator : Returns true or false
  • With eql? Operator : Returns true or false
  • With casecmp method : Returns 0 if matched or 1 if not matched

Question - 64 : - What are Ruby arrays and how they can be created?

Answer - 64 : -

Ruby arrays are ordered collections of objects. They can hold objects like integer, number, hash, string, symbol or any other array.

Its indexing starts with 0. The negative index starts with -1 from the end of the array. For example, -1 indicates last element of the array and 0 indicates first element of the array.

A Ruby array is created in many ways.

Using literal constructor []
Using new class method

Question - 65 : - How to access Ruby array elements? How many methods are used to access Ruby elements.?

Answer - 65 : -

Ruby array elements can be accessed using #[] method. You can pass one or more than one arguments or even a range of arguments.

Syntax:

#[] method  
Methods used to access Ruby elements:

  • at method
  • slice method
  • fetch method
  • first and last method
  • take method
  • drop method

Question - 66 : - In how many ways items can be added in an array in Ruby?

Answer - 66 : -

Ruby array elements can be added in different ways.

  • push or <<
  • unshift
  • insert

Question - 67 : - In how many ways items can be removed from array in Ruby?

Answer - 67 : -

Ruby array elements can be removed in different ways.

  • pop
  • shift
  • delete
  • uniq

Question - 68 : - Explain Ruby hashes.

Answer - 68 : -

A Ruby hash is a collection of unique keys and their values. They are similar to arrays but array use integer as an index and hash use any object type. They are also called associative arrays, dictionaries or maps.

If a hash is accessed with a key that does not exist, the method will return nil.

Question - 69 : - How to create a new time instance in Ruby?

Answer - 69 : -

A new Time instance can be created with ::new. This will use your current system's time. Parts of time like year, month, day, hour, minute, etc can also be passed.

While creating a new time instance, you need to pass at least a year. If only year is passed, then time will default to January 1 of that year at 00:00:00 with current system time zone.

Question - 70 : - Explain Ruby ranges. What are the ways to define ranges?

Answer - 70 : -

Ruby range represents a set of values with a beginning and an end. They can be constructed using s..e and s...e literals or with ::new.

The ranges which has .. in them, run from beginning to end inclusively. The ranges which has ... in them, run exclusively the end value.

Ruby has a variety of ways to define ranges.

  • Ranges as sequences
  • Ranges as conditions
  • Ranges as intervals


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners