Quiz 1.1: Basic Syntax


Q1: What is it stored in a project file ?

What information can be stored on a Julia Project.toml file ?

RESOLUTION

The Project.toml file task is to indicate which is the set of packages that works with the given project, but not the concrete istance of the environment that is used in a project, that is the exact version of all directly and indirectly used packages. This is indeed the task of the Manifest.toml file.

The correct answers are:

  • "The name of the packages directly used in the project (julia scripts)"
  • "The ID of the packages directly used in the project (julia scripts)"
  • "The minimum and maximum version of the packages directly used in the project (julia scripts) that are compatible with the project"

Q2: Syntax for comments

Given the following sequence of commands (one for each line) run in an interactive session:

# a = 1
a = 2 # hello
a = # hello # 3
#= a = 4
#= a = 5 =#
a = 6
=#

Which statements are correct ?

RESOLUTION

The first command is a comment. On the second one, a is assigned the value 2. The third one raises a syntax error as the equal operator expects a right and a left hand side, while here the right hand side is all commented out. Finally lines 4 to the end is a big nested comment. It results that after that commands have been run, a remains assigned to 2. The correct answers are:

  • "a is now 2"
  • "At least one of that commands raises a run-time error"

Q3: Various syntax rules

Given a file "Foo.jl" with the following code:

function foo(x)
println(x²)
end
a = [2,3]
foo(a)
foo.(a)
foo(a[1])

Which of the following statements are correct ?

RESOLUTION

First, Unicode characters are allowed (with very rare exceptions) and identation doesn't matter in Julia. We would then be tempted to say hence that the broadcasted call foo.(a) produces [4,9] as output and foo(a[1]) produces 4. However the rising to the power is not obtained by using the Unicode ² character, but using the exponential operator, i.e. x^2. is just an other idetifier name that has not been defined, so the function in all cases returns an error that is not defined. The correct answer is:

  • "Calling the function produces a run-time error because is not defined"