Question 1. Which of the following defines a variable x
with value 10
in Julia?
Auto-evaluation
We have included in this page a series of quizzes to help reinforce your learning. These quizzes are designed not only to test your understanding but also to provide you with an opportunity for self-reflection. After each quiz, take a moment to review your answers, reflect on what you’ve learned, and identify areas where you may need further clarification. Do not hesitate to reach out to your lecturer if any questions come up.
Core Syntax
Question 2. Which keyword is used to define a function in Julia?
Question 3. Which symbol is used for element-wise operations in Julia (e.g., squaring each element of a vector)?
Question 4. What is the output of this code?
= [1, 2, 3]
a = a
b 1] = 10
b[println(a)
Question 5. What does the end
keyword do in Julia?
Question 6. Which expression returns the number of elements in a vector v
?
Question 7. What does ===
do in Julia?
Question 8. How do you stop a loop early in Julia?
Question 9. How do you skip the current iteration of a loop in Julia?
Question 10. Which of the following is a valid for
loop in Julia?
Type Hierarchies
Question 1. What is the purpose of an abstract type in Julia?
Question 2. Which of the following types is a concrete type?
Question 3. What does the isconcretetype
function return for AbstractFloat
?
Question 4. What will the following code return?
typeof(42)
Question 5. What is the purpose of the isa
operator in Julia?
Question 6. What will be the result of the following code?
Int64 <: Real
Question 7. What will the following code return?
isconcretetype(Int64)
isconcretetype(AbstractFloat)
Question 8. What does the <:
operator check in Julia?
Question 9. What is the result of the following code?
subtypes(Real)
Question 10. What does the supertype
function return for Float64
?
supertype(Float64)
Type Conversion and Promotion
Question 1. What does the convert
function do in Julia?
Question 2. What is the output of the following code?
println(round(Int, 3.14)) # Rounds 3.14 to the nearest integer, output: 3
println(floor(Int, 3.14)) # Floors 3.14 to the nearest integer, output: 3
println(convert(Float64, 5)) # Converts Int to Float64, output: 5.0
println(string(123)) # Converts Int to String, output: "123"
Question 3. What happens when an integer is assigned to a Float64
variable in Julia?
::Float64 = 10 # The integer 10 is automatically converted to 10.0 (Float64)
yprintln(y) # Output: 10.0
Question 4. What does the promote
function do in Julia?
= promote(3, 4.5) # Promotes both values to Float64
a, b println(a) # Output: 3.0
println(b) # Output: 4.5
println(typeof(a)) # Output: Float64
println(typeof(b)) # Output: Float64
Question 5. What will happen if we try to add an Int
and a String
in Julia?
println(3 + "Hello") # Attempting to add Int and String
Special Types
Question 1. What does the Nothing
type represent in Julia?
Question 2. What is the result of calling the following function in Julia?
# Example of a function that returns `Nothing`
function print_message(msg::String)
println(msg)
return nothing # Explicitly returns `nothing`
end
= print_message("Hello!")
result println(result === nothing) # Output: true
Question 3. What is the advantage of using Any
as a type in Julia?
# Example of using `Any` as a type
function describe(value::Any)
println("Value: ", value)
println("Type: ", typeof(value))
end
describe(42) # Works with Int
describe("Hello") # Works with String
describe(3.14) # Works with Float64
Question 4. What does the following code do in Julia?
= [1, 2, missing, 4, 5]
data for item in data
if item === missing
println("Missing data detected.")
else
println("Value: ", item)
end
end
Question 5. What is the purpose of the skipmissing
function in Julia?
using Statistics
# Example array with missing values
= [1, 2, missing, 4, 5, missing, 7]
data
# Summing values while skipping missing entries
= sum(skipmissing(data))
sum_no_missing println("Sum without missing values: ", sum_no_missing) # Output: 19
# Calculating the mean while skipping missing values
= mean(skipmissing(data))
mean_no_missing println("Mean without missing values: ", mean_no_missing) # Output: 3.8
Question 6. What is the main use of the Missing
type in Julia?
# Example of using `missing` in an array
= [1, 2, missing, 4, 5]
data
# Check for missing values in the array
for item in data
if item === missing
println("Missing data detected.")
else
println("Value: ", item)
end
end
Union Types
Question 1. What is a Union
type in Julia?
Question 2. What is the output of the following code?
function process_number(x::Union{Int, Float64})
println("The input is: ", x)
end
process_number(5) # Works with an Int
process_number(3.14) # Works with a Float64
Question 3. Which of the following scenarios would benefit from using a Union
type?
# Example using Union to handle multiple types in a function
function add_one(x::Union{Int, Float64})
return x + 1
end
println(add_one(3)) # Output: 4 (Int)
println(add_one(2.5)) # Output: 3.5 (Float64)
Question 4. What happens when a value of a type not listed in the Union
is passed to a function?
function process_number(x::Union{Int, Float64})
println("The input is: ", x)
end
process_number("Hello") # Trying to pass a String
Question 5. How does the add_one
function handle both Int
and Float64
types?
function add_one(x::Union{Int, Float64})
return x + 1
end
println(add_one(3)) # Output: 4 (Int)
println(add_one(2.5)) # Output: 3.5 (Float64)
Type Annotations and Declarations
Question 1. What is the primary purpose of type annotations in Julia?
Question 2. Which of the following correctly applies a type annotation to a variable?
Question 3. What will happen if the following code is executed?
function add(a::Int, b::Int)
return a + b
end
add(3, "4")
Question 4. In Julia, what will the following code output?
function multiply(a::Int, b::Int)::Int
return a * b
end
multiply(3, 4)
Parametric Types
Question 1. What is a parametric type in Julia?
Question 2. What is the role of T
and S
in the Pair
struct example?
struct Pair{T, S}
::T
first::S
secondend
= Pair(1, "apple") # Pair of Int and String
pair1 = Pair(3.14, true) # Pair of Float64 and Bool pair2
Question 3. What happens when you instantiate Pair(1, 'apple')
in the provided code?
= Pair(1, "apple") # Pair of Int and String pair1
Question 4. What is the benefit of using parametric types like AbstractContainer{T}
?
abstract type AbstractContainer{T} end
struct VectorContainer{T} <: AbstractContainer{T}
::Vector{T}
dataend
struct SetContainer{T} <: AbstractContainer{T}
::Set{T}
dataend
Question 5. What does the print_container_info
function do?
function print_container_info(container::AbstractContainer{T}) where T
println("Container holds values of type: ", T)
end
Question 6. What is the purpose of AbstractContainer{T}
in the code example?
abstract type AbstractContainer{T} end
Question 7. What would be the output of print_container_info(vec)
if vec
is VectorContainer([1, 2, 3])
?
= VectorContainer([1, 2, 3]) vec
Question 8. How does using parametric types help with code reusability?
Errors and Exception Handling
Question 1. Which error type is raised when an index is out of bounds in an array?
Question 2. What does the following code do in Julia?
function divide(a, b)
if b == 0
throw(DivideError())
end
return a / b
end
Question 3. What happens when the following try
/catch
block is executed?
try
println(divide(10, 0)) # Will raise an error
catch e
println("Error: ", e) # Handles the error
end
Question 4. What is the purpose of the finally
block in Julia’s exception handling?
Question 5. What is the output of the following code?
function safe_file_read(filename::String)
= nothing
file try
= open(filename, "r")
file = read(file, String)
data return data
catch e
println("An error occurred: ", e)
finally
if file !== nothing
close(file)
println("File closed.")
end
end
end
# Test with a valid file
println(safe_file_read("example.txt"))
# Test with an invalid file
println(safe_file_read("nonexistent.txt"))
Question 6. Which of the following is an appropriate use case for the finally
block?
Performance
Question 1. What does it mean for a function in Julia to be type stable?
Question 2. Which Julia macro is commonly used to check for type stability?
Question 3. Which of the following best describes a type-unstable function?
Question 4. Consider the function:
function g(x)
return x > 10 ? 10 : "small"
end
Question 5. In Julia, which type causes the biggest problems for type inference when used as a return type?
Question 6. Which of the following is volatile (non permanent) memory?
Question 7. Which component temporarily stores instructions and data for quick access by the CPU?
Question 8. he basic unit of data in computer hardware is: