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 1. Which of the following defines a variable x with value 10 in Julia?

Select an item

Question 2. Which keyword is used to define a function in Julia?

Select an item

Question 3. Which symbol is used for element-wise operations in Julia (e.g., squaring each element of a vector)?

Select an item

Question 4. What is the output of this code?

a = [1, 2, 3]
b = a
b[1] = 10
println(a)

Select an item

Question 5. What does the end keyword do in Julia?

Select an item

Question 6. Which expression returns the number of elements in a vector v?

Select an item

Question 7. What does === do in Julia?

Select an item

Question 8. How do you stop a loop early in Julia?

Select an item

Question 9. How do you skip the current iteration of a loop in Julia?

Select an item

Question 10. Which of the following is a valid for loop in Julia?

Select an item

Type Hierarchies

Question 1. What is the purpose of an abstract type in Julia?

Select an item

Question 2. Which of the following types is a concrete type?

Select an item

Question 3. What does the isconcretetype function return for AbstractFloat?

Select an item

Question 4. What will the following code return?

typeof(42)

Select an item

Question 5. What is the purpose of the isa operator in Julia?

Select an item

Question 6. What will be the result of the following code?

Int64 <: Real

Select an item

Question 7. What will the following code return?

isconcretetype(Int64)
isconcretetype(AbstractFloat)

Select an item

Question 8. What does the <: operator check in Julia?

Select an item

Question 9. What is the result of the following code?

subtypes(Real)

Select an item

Question 10. What does the supertype function return for Float64?

supertype(Float64)

Select an item

Type Conversion and Promotion

Question 1. What does the convert function do in Julia?

Select an item

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"

Select an item

Question 3. What happens when an integer is assigned to a Float64 variable in Julia?

y::Float64 = 10  # The integer 10 is automatically converted to 10.0 (Float64)
println(y)       # Output: 10.0

Select an item

Question 4. What does the promote function do in Julia?

a, b = promote(3, 4.5)  # Promotes both values to Float64
println(a)              # Output: 3.0
println(b)              # Output: 4.5
println(typeof(a))      # Output: Float64
println(typeof(b))      # Output: Float64

Select an item

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

Select an item

Special Types

Question 1. What does the Nothing type represent in Julia?

Select an item

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

result = print_message("Hello!")
println(result === nothing)  # Output: true

Select an item

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

Select an item

Question 4. What does the following code do in Julia?

data = [1, 2, missing, 4, 5]
for item in data
    if item === missing
        println("Missing data detected.")
    else
        println("Value: ", item)
    end
end

Select an item

Question 5. What is the purpose of the skipmissing function in Julia?

using Statistics

# Example array with missing values
data = [1, 2, missing, 4, 5, missing, 7]

# Summing values while skipping missing entries
sum_no_missing = sum(skipmissing(data))
println("Sum without missing values: ", sum_no_missing)  # Output: 19

# Calculating the mean while skipping missing values
mean_no_missing = mean(skipmissing(data))
println("Mean without missing values: ", mean_no_missing)  # Output: 3.8

Select an item

Question 6. What is the main use of the Missing type in Julia?

# Example of using `missing` in an array
data = [1, 2, missing, 4, 5]

# Check for missing values in the array
for item in data
    if item === missing
        println("Missing data detected.")
    else
        println("Value: ", item)
    end
end

Select an item

Union Types

Question 1. What is a Union type in Julia?

Select an item

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

Select an item

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)

Select an item

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

Select an item

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)

Select an item

Type Annotations and Declarations

Question 1. What is the primary purpose of type annotations in Julia?

Select an item

Question 2. Which of the following correctly applies a type annotation to a variable?

Select an item

Question 3. What will happen if the following code is executed?

function add(a::Int, b::Int)
    return a + b
end

add(3, "4")

Select an item

Question 4. In Julia, what will the following code output?

function multiply(a::Int, b::Int)::Int
    return a * b
end
multiply(3, 4)

Select an item

Parametric Types

Question 1. What is a parametric type in Julia?

Select an item

Question 2. What is the role of T and S in the Pair struct example?

struct Pair{T, S}
    first::T
    second::S
end

pair1 = Pair(1, "apple")  # Pair of Int and String
pair2 = Pair(3.14, true)  # Pair of Float64 and Bool

Select an item

Question 3. What happens when you instantiate Pair(1, 'apple') in the provided code?

pair1 = Pair(1, "apple")  # Pair of Int and String

Select an item

Question 4. What is the benefit of using parametric types like AbstractContainer{T}?

abstract type AbstractContainer{T} end

struct VectorContainer{T} <: AbstractContainer{T}
    data::Vector{T}
end

struct SetContainer{T} <: AbstractContainer{T}
    data::Set{T}
end

Select an item

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

Select an item

Question 6. What is the purpose of AbstractContainer{T} in the code example?

abstract type AbstractContainer{T} end

Select an item

Question 7. What would be the output of print_container_info(vec) if vec is VectorContainer([1, 2, 3])?

vec = VectorContainer([1, 2, 3])

Select an item

Question 8. How does using parametric types help with code reusability?

Select an item

Errors and Exception Handling

Question 1. Which error type is raised when an index is out of bounds in an array?

Select an item

Question 2. What does the following code do in Julia?

function divide(a, b)
    if b == 0
        throw(DivideError())
    end
    return a / b
end

Select an item

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

Select an item

Question 4. What is the purpose of the finally block in Julia’s exception handling?

Select an item

Question 5. What is the output of the following code?

function safe_file_read(filename::String)
    file = nothing
    try
        file = open(filename, "r")
        data = read(file, String)
        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"))

Select an item

Question 6. Which of the following is an appropriate use case for the finally block?

Select an item

Performance

Question 1. What does it mean for a function in Julia to be type stable?

Select an item

Question 2. Which Julia macro is commonly used to check for type stability?

Select an item

Question 3. Which of the following best describes a type-unstable function?

Select an item

Question 4. Consider the function:

function g(x)
    return x > 10 ? 10 : "small"
end

Select an item

Question 5. In Julia, which type causes the biggest problems for type inference when used as a return type?

Select an item

Question 6. Which of the following is volatile (non permanent) memory?

Select an item

Question 7. Which component temporarily stores instructions and data for quick access by the CPU?

Select an item

Question 8. he basic unit of data in computer hardware is:

Select an item
Back to top