Frequently asked questions and answers of Crystal in Programming Technologies of Computer Science to enhance your skills, knowledge on the selected topic. We have compiled the best Crystal Interview question and answer, trivia quiz, mcq questions, viva question, quizzes to prepare. Download Crystal FAQs in PDF form online for academic course, jobs preparations and for certification exams .
Intervew Quizz is an online portal with frequently asked interview, viva and trivia questions and answers on various subjects, topics of kids, school, engineering students, medical aspirants, business management academics and software professionals.
Question-1. What is Crystal?
Answer-1: Crystal is a statically typed, compiled programming language inspired by Ruby, designed for performance and concurrency.
Question-2. What are the key features of Crystal?
Answer-2: Crystal offers a Ruby-like syntax, static typing with type inference, high performance, and built-in support for concurrency.
Question-3. How does Crystal handle type inference?
Answer-3: Crystal automatically infers types based on the values assigned, reducing the need for explicit type annotations.
Question-4. How does Crystal compare to Ruby?
Answer-4: Crystal has a Ruby-like syntax but is statically typed, compiled, and much faster due to its compiled nature.
Question-5. How do you define a variable in Crystal?
Answer-5: Variables are defined using var or without explicit types, e.g., name = "Crystal" or age : Int32 = 25.
Question-6. How do you define a method in Crystal?
Answer-6: Methods are defined using the def keyword, e.g., def greet(name : String) : String; "Hello, #{name}"; end.
Question-7. What is Crystal's concurrency model?
Answer-7: Crystal uses fibers, which are lightweight threads, for concurrency.
Question-8. How do you write a loop in Crystal?
Answer-8: Loops can be written using while, until, or iterators like each, e.g., `5.times {
Question-9. What is the purpose of macros in Crystal?
Answer-9: Macros are used for metaprogramming, allowing code generation and manipulation at compile-time.
Question-10. How do you create a class in Crystal?
Answer-10: Classes are created using the class keyword, e.g., class Person; def initialize(name : String); @name = name; end; end.
Question-11. What are modules in Crystal?
Answer-11: Modules are collections of methods and constants, used to share functionality across classes.
Question-12. How do you include a module in a class in Crystal?
Answer-12: Modules are included using the include keyword, e.g., include Comparable.
Question-13. What is the require keyword used for in Crystal?
Answer-13: The require keyword is used to include external libraries or other files into a Crystal program.
Question-14. How do you define an array in Crystal?
Answer-14: Arrays are defined using square brackets, e.g., arr = [1, 2, 3].
Question-15. How do you define a hash in Crystal?
Answer-15: Hashes are defined using curly braces, e.g., hash = {"key" => "value"}.
Question-16. How does Crystal handle exceptions?
Answer-16: Crystal uses begin, rescue, and ensure blocks for exception handling.
Question-17. What are blocks in Crystal?
Answer-17: Blocks are anonymous pieces of code passed to methods, enclosed in {} or do...end.
Question-18. How do you define a constant in Crystal?
Answer-18: Constants are defined using uppercase names, e.g., PI = 3.14.
Question-19. What is Crystal's type system?
Answer-19: Crystal has a static type system with union types and type inference.
Question-20. How do you use generics in Crystal?
Answer-20: Generics are defined using
Question-21. How do you perform string interpolation in Crystal?
Answer-21: String interpolation is done using #{} within double quotes, e.g., "Hello, #{name}".
Question-22. What are fibers in Crystal?
Answer-22: Fibers are lightweight threads that provide concurrency by running tasks cooperatively.
Question-23. How do you handle file I/O in Crystal?
Answer-23: File I/O is handled using the File module, e.g., `File.open("file.txt", "r") do
Question-24. How do you use annotations in Crystal?
Answer-24: Annotations are metadata for methods or classes, used for compile-time directives, e.g., @[Deprecated("Use new_method instead")].
Question-25. How do you implement an interface in Crystal?
Answer-25: Interfaces are implemented using abstract classes or modules with method definitions.
Question-26. What are structs in Crystal?
Answer-26: structs are value types used for lightweight objects, defined using the struct keyword.
Question-27. How do you work with command-line arguments in Crystal?
Answer-27: Command-line arguments are accessed using the ARGV array.
Question-28. How do you define an enum in Crystal?
Answer-28: Enums are defined using the enum keyword, e.g., enum Color; Red; Green; Blue; end.
Question-29. What are shards in Crystal?
Answer-29: Shards are Crystal's package management system, allowing libraries to be easily shared and installed.
Question-30. How do you handle JSON in Crystal?
Answer-30: JSON is handled using the JSON module, e.g., JSON.parse(string) or to_json.
Question-31. What are unions in Crystal?
Answer-31: Unions represent a value that can be of multiple types, e.g., `String
Question-32. What is a nil type in Crystal?
Answer-32: nil represents the absence of a value, similar to null in other languages.
Question-33. How do you use regular expressions in Crystal?
Answer-33: Regular expressions are created using %r(), e.g., regex = %r{[a-z]+}.
Question-34. What is the purpose of the yield keyword in Crystal?
Answer-34: yield passes control from a method to a block provided to the method.
Question-35. How do you define a singleton method in Crystal?
Answer-35: Singleton methods are defined on the metaclass, e.g., def self.method_name; end.
Question-36. How do you handle concurrency in Crystal?
Answer-36: Concurrency is managed using fibers and channels for communication.
Question-37. What is a channel in Crystal?
Answer-37: Channels are used for communication between fibers, providing thread-safe message passing.
Question-38. How do you format strings in Crystal?
Answer-38: Strings are formatted using the sprintf method or interpolation.
Question-39. How do you create custom exceptions in Crystal?
Answer-39: Custom exceptions are created by inheriting from Exception.
Question-40. How does Crystal manage memory?
Answer-40: Crystal uses automatic memory management with a garbage collector.
Question-41. How do you use the select method in Crystal?
Answer-41: select is used with channels to wait for multiple communications.
Question-42. How do you define a range in Crystal?
Answer-42: Ranges are defined using .. or ..., e.g., 1..10.
Question-43. What is the nilable type in Crystal?
Answer-43: A nilable type is a union of nil and another type, e.g., String?.
Question-44. How do you create a library in Crystal?
Answer-44: Libraries are created by defining reusable modules or classes and publishing them as shards.
Question-45. How do you test Crystal code?
Answer-45: Crystal code is tested using the built-in Spec module.
Question-46. How do you write a macro in Crystal?
Answer-46: Macros are written using {% %} for compile-time code evaluation.
Question-47. How do you optimize performance in Crystal?
Answer-47: Performance is optimized by leveraging compile-time checks, static typing, and avoiding runtime type checks.
Question-48. What is the purpose of require "spec" in Crystal?
Answer-48: require "spec" includes the testing framework for writing unit tests.
Question-49. How do you perform type casting in Crystal?
Answer-49: Type casting is done using as, e.g., x = obj as Int32.
Question-50. How do you run a Crystal program?
Answer-50: Crystal programs are run using crystal run filename.cr.
Frequently Asked Question and Answer on Crystal
Crystal Interview Questions and Answers in PDF form Online
Crystal Questions with Answers
Crystal Trivia MCQ Quiz