Ruby - ArraysWhich of the following is the correct syntax to slice the first 4 elements from an array arr in Ruby?Aarr[1..4]Barr[0...4]Carr[0..4]Darr[1...5]Check Answer
Step-by-Step SolutionSolution:Step 1: Identify indices for first 4 elementsIndices start at 0, so first 4 elements are at indices 0, 1, 2, 3.Step 2: Choose range that includes indices 0 to 3arr[0...4] excludes index 4, selecting indices 0, 1, 2, 3 exactly.Final Answer:arr[0...4] -> Option BQuick Check:Use 0...4 to get first 4 elements (indices 0 to 3) [OK]Quick Trick: Use 0...n to get first n elements [OK]Common Mistakes:Starting index at 1 instead of 0Using .. which includes one extra elementConfusing inclusive and exclusive ranges
Master "Arrays" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Arrays - Compact for removing nil values - Quiz 9hard Arrays - Array modification (push, pop, shift, unshift) - Quiz 10hard Loops and Iteration - Why Ruby prefers iterators over loops - Quiz 6medium Loops and Iteration - Until loop - Quiz 6medium Methods - Parameters with default values - Quiz 2easy Methods - Why methods always return a value in Ruby - Quiz 15hard Methods - Predicate methods (ending with ?) - Quiz 15hard String Operations - String methods (upcase, downcase, strip) - Quiz 6medium Variables and Data Types - Local variables and naming conventions - Quiz 11easy Variables and Data Types - Type checking with .class and .is_a? - Quiz 1easy