Basic Types and Variables Flashcards
What are the main technological areas that have seen significant advancements over the past 20 years?
Mobile devices, the cloud, artificial intelligence, and self-driving cars.
What was the analytical engine?
The analytical engine was an early computing device that used mechanical gears and was computed by manually moving hand cranks.
What is binary code?
Binary code consists of two digits, 0 and 1, which represent different electrical states in a computer.
Example: 0 is off, and 1 is on.
Why do computers use binary code?
Computers only understand binary code because it represents two states (on and off) which are easy to represent with electrical signals.
What is an example of decimal to binary conversion?
Decimal 1 is binary 1, Decimal 2 is binary 10, Decimal 3 is binary 11.
What is the CPU?
The CPU, or Central Processing Unit, is the brain of the computer that processes instructions and performs calculations.
What happens when a program is written in any language?
It needs to be compiled or interpreted into machine-readable code (binary) so that the computer can execute it.
Why don’t humans program in binary?
Binary code is hard to read and error-prone for humans, so we use high-level programming languages instead.
What is programming?
Programming is the process of providing a computer with a set of instructions in a particular language that it can understand to perform tasks.
How does one improve at programming?
Programming is a skill that improves with practice. The more you write code, the better you become at applying logic and conditions.
Why is programming considered a creative skill?
Programming is creative because there are many different ways to solve problems through coding.
What are constants and variables in programming?
Constants and variables allow you to store and organize values of the same type. Variables can change values, while constants cannot.
Example in Swift:
let pi = 3.14
var age = 25
How do you declare a constant in Swift?
You declare a constant using the keyword ‘let’ followed by the constant name, assignment operator ‘=’, and a value.
Example:
let pi = 3.14
How do you declare a variable in Swift?
You declare a variable using the keyword ‘var’ followed by the variable name, assignment operator ‘=’, and a value.
Example:
var age = 25
Why can’t a constant’s value be changed in Swift?
A constant’s value can’t be changed because it’s declared as a fixed value with the ‘let’ keyword, which makes it immutable.
Why would you use a variable instead of a constant?
You would use a variable if the value needs to change over time, such as storing a user’s age, which changes annually.
What happens if you declare two variables with the same name?
If you declare two variables or constants with the same name, Swift will produce an error.
What naming restrictions exist for Swift variables and constants?
Variable and constant names cannot start with a number or use mathematical symbols.
What are data types in Swift?
Data types describe the type of value a variable or constant will store, such as integers, floats, Booleans, and strings.
What is an integer in Swift?
An integer represents whole numbers, and its type is ‘Int’.
Example:
let numberOfItems = 10
What is a floating point in Swift?
A floating point represents numbers with a decimal, and its type is ‘Float’ or ‘Double’ for more precision.
Example:
let price = 19.99
What is the difference between ‘Float’ and ‘Double’ in Swift?
A ‘Float’ has 32 bits of storage for representing numbers, while a ‘Double’ has 64 bits, allowing it to represent larger numbers.
What is the Boolean data type?
A Boolean data type represents logical values: true or false.
Example:
let isAuthenticated = true
What is type inference in Swift?
Type inference means Swift can automatically determine the data type based on the value assigned to a variable or constant.
Example:
let score = 100 (Swift will infer ‘score’ is of type Int).
What does it mean that Swift is a type-safe language?
Type safety in Swift means you cannot mix different data types, and Swift checks data types before compiling the code to avoid errors.
What are operators in programming?
Operators are used to perform operations on variables and values, manipulating data and returning results.
Example in Swift:
let sum = 5 + 3 // sum equals 8
What is the assignment operator?
The assignment operator ‘=’ assigns a value to a variable.
Example in Swift:
var age = 25
What are mathematical operators in Swift?
Mathematical operators include addition (+), subtraction (-), multiplication (*), and division (/).
Example in Swift:
let result = 5 * 3 // result equals 15
What is a compound assignment operator?
Compound assignment operators are shorthand for operations. For example, instead of writing ‘x = x + 2’, you can write ‘x += 2’.
Example in Swift:
var count = 5
count += 2 // count equals 7
What is the modulus operator?
The modulus operator (%) returns the remainder of a division.
Example in Swift:
let remainder = 10 % 3 // remainder equals 1
How does Swift handle floating point arithmetic?
Swift automatically assigns floating point numbers to the Double type.
Example in Swift:
let value = 3.3 + 1.5 // value equals 4.8
What is the unary minus operator in Swift?
The unary minus operator allows you to convert a number from positive to negative and vice versa.
Example in Swift:
let negative = -5 // negative equals -5
What is numeric type conversion in Swift?
Numeric type conversion allows you to change the type of a variable to combine different types (e.g., Int and Double).
Example in Swift:
let sum = Double(5) + 0.3 // sum equals 5.3
Why can’t different numeric types be added together in Swift?
Swift is a strongly typed language, meaning different types (e.g., Int and Double) can’t be combined without conversion.
Example:
let sum = Double(5) + 0.3
What is BIDMAS in Swift?
BIDMAS stands for Brackets, Indices, Division, Multiplication, Addition, and Subtraction. It represents the order of operations Swift follows for mathematical expressions.
Example:
let result = (2 + 3) * 4 // result equals 20
What are operators in programming?
Operators are used to perform operations on variables and values, manipulating data and returning results.
Example in Swift:
let sum = 5 + 3 // sum equals 8
What is the assignment operator?
The assignment operator ‘=’ assigns a value to a variable.
Example in Swift:
var age = 25
What are mathematical operators in Swift?
Mathematical operators include addition (+), subtraction (-), multiplication (*), and division (/).
Example in Swift:
let result = 5 * 3 // result equals 15
What is a compound assignment operator?
Compound assignment operators are shorthand for operations. For example, instead of writing ‘x = x + 2’, you can write ‘x += 2’.
Example in Swift:
var count = 5
count += 2 // count equals 7
What is the modulus operator?
The modulus operator (%) returns the remainder of a division.
Example in Swift:
let remainder = 10 % 3 // remainder equals 1
How does Swift handle floating point arithmetic?
Swift automatically assigns floating point numbers to the Double type.
Example in Swift:
let value = 3.3 + 1.5 // value equals 4.8
What is the unary minus operator in Swift?
The unary minus operator allows you to convert a number from positive to negative and vice versa.
Example in Swift:
let negative = -5 // negative equals -5
What is numeric type conversion in Swift?
Numeric type conversion allows you to change the type of a variable to combine different types (e.g., Int and Double).
Example in Swift:
let sum = Double(5) + 0.3 // sum equals 5.3
Why can’t different numeric types be added together in Swift?
Swift is a strongly typed language, meaning different types (e.g., Int and Double) can’t be combined without conversion.
Example:
let sum = Double(5) + 0.3
What is BIDMAS in Swift?
BIDMAS stands for Brackets, Indices, Division, Multiplication, Addition, and Subtraction. It represents the order of operations Swift follows for mathematical expressions.
Example:
let result = (2 + 3) * 4 // result equals 20
What is a string in Swift?
A string is an ordered collection of characters surrounded by double quotes and is represented by the String type.
Example in Swift:
let greeting = “Hello, World!”
What is a string literal?
A string literal is the actual group of characters inside double quotes.
Example in Swift:
let myString = “Hello, World!” // “Hello, World!” is the string literal
What is string mutability in Swift?
If a string is assigned to a constant (let), it is immutable and can’t be changed. If assigned to a variable (var), it is mutable and can be changed.
Example:
var message = “Hi”
message = “Hello” // This works because it’s a variable
How can you concatenate strings in Swift?
You can concatenate (combine) two strings using the + operator.
Example in Swift:
let firstName = “John”
let lastName = “Doe”
let fullName = firstName + “ “ + lastName // “John Doe”
How can you compare strings in Swift?
You can compare strings using methods like hasPrefix and hasSuffix, which check if a string starts or ends with specific characters.
Example in Swift:
let greeting = “Hello, World!”
greeting.hasPrefix(“Hello”) // true
How can you convert strings to uppercase or lowercase in Swift?
You can use the uppercased() and lowercased() methods to convert a string to all uppercase or all lowercase characters.
Example in Swift:
let text = “Hello”
let upper = text.uppercased() // “HELLO”
How can you count the number of characters in a string?
You can use the count method to find the number of characters in a string.
Example in Swift:
let message = “Hi There”
let characterCount = message.count // 8
What are multiline string literals in Swift?
Multiline string literals allow you to store multiple lines of text using three double-quotes (“””).
Example:
let message = “””
This is a multiline string.
It spans several lines.
“””