Rust

Which type cast preserves the mathematical value in all cases?

1.

i64 as i32

2.

usize as u64

3.

i32 as i64

4.

f64 as f32

Q 1 / 32

Rust

What do the vertical bars represent here?

rust str::thread::spawn(|| { println!("LinkedIn"); });

1.

a closure

2.

a thread

3.

a future

4.

a block

Q 2 / 32

Rust

Which choice is not a scalar data type?

1.

integer

2.

float

3.

boolean

4.

tuple

Q 3 / 32

Rust

**_** cannot be destructured.

1.

Traits

2.

Tuples

3.

Enums

4.

Structs

Q 4 / 32

Rust

Which `cargo` command checks a program for error without creating a binary executable?

1.

cargo --version

2.

cargo init

3.

cargo build

4.

cargo check

Q 5 / 32

Rust

The term _box_ and related phrases such as _boxing a value_ are often used when relating to memory layout. What does _box_ refer to?

1.

It's creating a pointer on the heap that points to a value on the stack.

2.

It's creating a pointer on the stack that points to a value on the heap.

3.

It's creating a memory guard around values to prevent illegal access.

4.

It's an abstraction that refers to ownership. "Boxed" values are clearly labelled.

Q 6 / 32

Rust

What is an alternative way of writing `slice` that produces the same result?

rust ... let s = String::form("hello"); let slice = &s[0..2];

1.

let slice = &s[len + 2];

2.

let slice = &s[len - 2];

3.

let slice = &s.copy(0..2);

4.

let slice = &s[..2];

Q 7 / 32

Rust

Using the `?` operator at the end of an expression is equivalent to **_**.

1.

a match pattern that branches into True or False

2.

calling ok_error()

3.

calling panic!()

4.

a match pattern that may result an early return

Q 8 / 32

Rust

Which is valid syntax for defining an array of i32 values?

1.

Array<i32>::with_capacity(10)

2.

[i32]

3.

Array<i32>::new(10)

4.

[i32; 10]

Q 9 / 32

Rust

What syntax is required to take a mutable reference to T, when used within a function argument?

rust fn increment(i: T) { // body elided }

1.

*mut T

2.

mut ref T

3.

mut &T

4.

&mut T

Q 10 / 32

Rust

The smart pointers Rc and Arc provide reference counting. What is the API for incrementing a reference count?

1.

.add()

2.

.incr()

3.

.clone()

4.

.increment()

Q 11 / 32

Rust

What happens when an error occurs that is being handled by the question mark (?) operator?

1.

The error is reported and execution continues.

2.

An exception is raised. The effect(s) of the exception are defined by the error! macro.

3.

The program panics immediately.

4.

Rust attempts to convert the error to the local function's error type and return it as Result::Err. If that fails, the program panics.

Q 12 / 32

Rust

Which comment syntax is not legal?

1.

`/*`

2.

`#`

3.

`//!`

4.

`//`

Q 13 / 32

Rust

In matching patterns, values are ignored with _.

1.

`.ignore()`

2.

`an underscore (_)`

3.

..

4.

skip

Q 14 / 32

Rust

Defining a _ requires a lifetime parameter.

1.

function that ends the lifetime of one of its arguments

2.

struct that contains a reference to a value

3.

function with a generic argument

4.

struct that contains a reference to a boxed value

Q 15 / 32

Rust

Which example correctly uses std::collections::HashMap's Entry API to populate counts?

rust use std::collections::HashMap; fn main() { let mut counts = HashMap::new(); let text = "LinkedIn Learning"; for c in text.chars() { // Complete this block } println!("{:?}", counts); } rust for c in text.chars() { if let Some(count) = &mut counts.get(&c) { counts.insert(c, *count + 1); } else { counts.insert(c, 1); }; } rust for c in text.chars() { let count = counts.entry(c).or_insert(0); *count += 1; } rust for c in text.chars() { let count = counts.entry(c); *count += 1; } rust for c in text.chars() { counts.entry(c).or_insert(0).map(|x| x + 1); }

1.

rust for c in text.chars() { if let Some(count) = &mut counts.get(&c) { counts.insert(c, *count + 1); } else { counts.insert(c, 1); }; }

2.

rust for c in text.chars() { let count = counts.entry(c).or_insert(0); *count += 1; }

3.

rust for c in text.chars() { let count = counts.entry(c); *count += 1; }

4.

rust for c in text.chars() { counts.entry(c).or_insert(0).map(|x| x + 1); }

Q 16 / 32

Rust

Which fragment does not incur memory allocations while writing to a "file" (represented by a Vec<u8>)?

rust use std::collections::HashMap; fn main() -> Result<(), Box<dyn std::error::Error>> { let mut v = Vec::<u8>::new(); let a = "LinkedIn"; let b = 123; let c = '🧀'; // replace this line println!("{:?}", v); Ok(()) } rust write!(&mut v, "{}{}{}", a, b, c)?; rust v.write(a)?; v.write(b)?; v.write(c)?; rust v.write(a, b, c)?; rust v.write_all(a.as_bytes())?; v.write_all(&b.to_string().as_bytes())?; c.encode_utf8(&mut v); 1. [Answered in rust user forum](https://users.rust-lang.org/t/formatting-and-writing-to-a-file-without-malloc-or-locks/52295/9) 2. [reference](https://doc.rust-lang.org/std/macro.write.html)

1.

rust write!(&mut v, "{}{}{}", a, b, c)?;

2.

rust v.write(a)?; v.write(b)?; v.write(c)?;

3.

rust v.write(a, b, c)?;

4.

rust v.write_all(a.as_bytes())?; v.write_all(&b.to_string().as_bytes())?; c.encode_utf8(&mut v);

Q 17 / 32

Rust

Does the `main` function compile? If so, why? If not, what do you need to change?

rust fn main() { let Some(x) = some_option_value; }

1.

The code does not compile. `let` statements require a refutable pattern. Add `if` before `let`.

2.

The code compiles. `let` statements sometimes require a refutable pattern.

3.

The code does not compile. `let` statements requires an irrefutable pattern. Add `if` before `let`.

4.

The code compiles. `let` do not require a refutable pattern.

Q 18 / 32

Rust

Which statement about lifetimes is false?

1.

Lifetimes were redundantly specified in previous version of Rust.

2.

Lifetimes are specified when a struct is holding a reference to a value.

3.

Lifetimes are specified when certain values must outlive others.

4.

Lifetimes are always inferred by the compiler.

Q 19 / 32

Rust

When used as a return type, which Rust type plays a similar role to Python's `None`, JavaScript's `null`, or the `void` type in C/C++?

1.

`!`

2.

`None`

3.

`Null`

4.

`()`

Q 20 / 32

Rust

To convert a `Result` to an `Option`, which method should you use?

1.

`.as_option()`

2.

`.ok()`

3.

`.to_option()`

4.

`.into()`

Q 21 / 32

Rust

Which statement about the `Clone` and `Copy` traits is false?

1.

`Copy` is enabled for primitive, built-in types.

2.

Without `Copy`, Rust applies move semantics to a type's access.

3.

When using `Clone`, copying data is explicit.

4.

Until a type implements either `Copy` or `Clone`, its internal data cannot be copied.

Q 22 / 32

Rust

Why does this code _not_ compile?

rust fn returns_closure() -> dyn Fn(i32) -> i32 { |x| x + 1 }

1.

The returned `fn` pointer and value need to be represented by another trait.

2.

Closures are types, so they cannot be returned directly from a function.

3.

Closures are types and can be returned only if the concrete trait is implemented.

4.

Closures are represented by traits, so they cannot be a return type.

Q 23 / 32

Rust

What smart pointer is used to allow multiple ownership of a value in various threads?

1.

`Arc<T>`

2.

`Box<T>`

3.

Both `Arc<T>` and `Rc<T>` are multithread safe.

4.

`Rc<T>`

Q 24 / 32

Rust

Which types are _not_ allowed within an enum variant's body?

1.

zero-sized types

2.

structs

3.

trait objects

4.

floating-point numbers

Q 25 / 32

Rust

Which statement about this code is true?

rust fn main() { let c = 'z'; let heart_eyed_cat = '😻'; }

1.

Both are character literals.

2.

`heart_eyed_cat` is an invalid expression.

3.

`c` is a string literal and `heart_eyed_cat` is a character literal.

4.

Both are string literals.

Q 26 / 32

Rust

Your application requires a single copy of some data type T to be held in memory that can be accessed by multiple threads. What is the thread-safe wrapper type?

1.

`Mutex<Arc<T>>`

2.

`Rc<Mutex<T>>`

3.

`Arc<Mutex<T>>`

4.

`Mutex<Rc<T>>`

Q 27 / 32

Rust

Which idiom can be used to concatenate the strings `a`, `b`, `c`?

rust let a = "a".to_string(); let b = "b".to_string(); let c = "c".to_string();

1.

`String::from(a,b,c)`

2.

`format!("{}{}{}", a, b, c)`

3.

`concat(a,b,c)`

4.

`a + b + c`

Q 28 / 32

Rust

In this function. what level of access is provided to the variable `a`?

rust use std::fmt::Debug; fn report<T:Debug>(a: &T) { eprintln!("info: {:?}", a); }

1.

print

2.

read-only

3.

read/write

4.

debug

Q 29 / 32

Rust

Which choice is _not_ valid loop syntax?

1.

`loop`

2.

`for`

3.

`while`

4.

`do`

Q 30 / 32

Rust

How do you construct a value of `Status` that is initialized to `Waiting`?

rust enum Status { Waiting, Busy, Error(String), }

1.

`let s = Enum::new(Status::Waiting);`

2.

`let s = new Status::Waiting;`

3.

`let s = Status::Waiting;`

4.

`let s = Status::new(Waiting);`

Q 31 / 32

Rust

Which statement about enums is false?

1.

Enums are useful in matching patterns.

2.

Option is an enum type.

3.

Enum variants can have different types with associated data.

4.

the term _enum_ is short for _enummap_

Q 32 / 32