Any2Quiz
Quizzes / Computer Science / Languages / Swift

Syntax Quiz - Languages Swift

Free practice quiz: Syntax (Languages Swift). 50 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

This Languages Swift practice quiz has 50 multiple-choice questions on Syntax, and 50 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.

Start quiz: 50 questions, free

Sample questions

  1. Which keyword is used to declare a constant in Swift?

    Show answer

    Answer: B. let

    In Swift, the `let` keyword is used to declare a constant, which means its value cannot be changed after it's initially assigned. The `var` keyword is used for variables.

  2. How do you declare a property within a struct in Swift?

    Show answer

    Answer: C. Both var and let propertyName: DataType

    Properties within a Swift struct can be declared using either the `var` keyword for mutable properties or the `let` keyword for constants.

  3. What is the correct way to declare an implicitly unwrapped optional String in Swift?

    Show answer

    Answer: B. var name: String!

    An implicitly unwrapped optional is declared by appending an exclamation mark (`!`) to the type. It's treated like an optional but can be accessed directly without unwrapping, assuming it always has a value.

  4. Which syntax is used for accessing a named element in a Swift tuple?

    Show answer

    Answer: C. myTuple.name

    If a tuple has named elements, you can access them using dot notation with their names (e.g., `myTuple.name`). Unnamed elements are accessed using their zero-based index (e.g., `myTuple.0`).

  5. What is the Swift keyword used to refer to the current instance of a type?

    Show answer

    Answer: A. self

    The `self` keyword is used within an instance method or computed property to refer to the current instance of the type.

Take the full quiz

More questions in this quiz

Answers and explanations are in the full quiz.

  1. What is the correct syntax for printing a string to the console in Swift?
  2. Which symbol denotes a single-line comment in Swift?
  3. How do you declare an optional String variable that can hold either a String or nil in Swift?
  4. What is the correct way to define a function that takes no arguments and returns no value in Swift?
  5. Which keyword is used to define a mutable variable in Swift?
  6. What is the Swift syntax for an integer literal?
  7. How do you define a class in Swift?
  8. What is the Swift syntax for a boolean literal representing true?
  9. Which operator is used for string concatenation in Swift?
  10. What does the `!` symbol signify when used after a variable name in Swift (e.g., `myVariable!`)?
  11. What is the Swift keyword for defining a closure?
  12. Which type inference feature allows Swift to automatically determine the type of a variable without explicit declaration?
  13. What is the Swift syntax for creating an empty array of integers?
  14. How do you access elements in a Swift Array using their index?
  15. Which keyword is used to define a struct in Swift?
  16. What is the Swift syntax for an empty string literal?
  17. What is the purpose of the `guard` statement in Swift?
  18. How do you define a dictionary with String keys and Int values in Swift?
  19. Which Swift keyword is used to define an enumeration?
  20. What is the Swift syntax for a floating-point literal (e.g., a Double)?
  21. How do you declare a tuple in Swift?
  22. What does the `inout` keyword signify when used as a parameter attribute in a Swift function?
  23. What is the Swift syntax for a range that includes both the start and end values (e.g., 1 to 5 inclusive)?
  24. Which control flow statement is used to iterate over a sequence (like an array or range) in Swift?
  25. What is the Swift keyword for defining a protocol?
  26. How do you create an empty dictionary with String keys and Int values in Swift?
  27. What is the purpose of the `defer` statement in Swift?
  28. What is the Swift syntax for creating a function that returns a tuple?
  29. Which Swift keyword is used to define an extension on an existing type?
  30. What is the Swift syntax for a character literal?
  31. Which of the following is NOT a valid Swift variable declaration?
  32. How do you declare an array of strings with an initial value in Swift?
  33. What is the Swift syntax for a range that includes the start but excludes the end value (e.g., 1 to 5, not including 5)?
  34. Which Swift keyword is used to define an initializer for a class or struct?
  35. What is the Swift syntax for a multiline comment?
  36. How do you define a parameter with a default value in a Swift function?
  37. Which Swift keyword is used to define a computed property?
  38. What is the correct Swift syntax for an empty dictionary literal?
  39. How do you create an optional Int variable that is guaranteed to have a value at runtime in Swift?
  40. Which keyword is used to define a property observer that is called after a property's value has been changed?
  41. How do you declare an array of any type in Swift (an array of Any)?
  42. What is the Swift syntax for a Boolean literal representing false?
  43. Which of the following is NOT a valid Swift syntax for a type alias?
  44. How do you declare a dictionary with string keys and string values in Swift?
  45. What is the purpose of the `switch` statement in Swift?

More quizzes

Browse all Swift quizzes

More to play

Daily ChallengeA new quiz every day. Keep your streak.HangmanGuess the word, letter by letter.Story QuestLearn through short interactive stories.All quizzesCBSE, ICSE, competitive exams and more.