Rust (Programming Language) LinkedIn Skill Assessment Quiz Answer

Rust (Programming Language)

 
Question
The smart pointers Rc and Arc provide reference counting. What is
the API for incrementing a reference count?
  • .clone()
  • .add()
  • .incr()
  • .increment()
Question 2)
The term box and related phrases such as boxing a value are often
used when relating to memory layout. What does box refer to?
  • It’s creating a pointer on the heap that points to a value on
    the stack.
  • It’s creating a memory guard around values to prevent illegal
    access.
  • It’s creating a pointer on the stack that points to a value on
    the heap.
  • It’s an abstraction that refers to ownership. “Boxed” values
    are clearly labelled.
Question 3)
Which type cast preserves the mathematical value in all cases?
  • i64 as i32
  • f64 as f32
  • usize as u64
  • i32 as i64
 
Question 4)
What do the vertical bars represent here?

str::thread::spawn(|| {
    println!("LinkedIn");
});
  • a future
  • a block
  • a closure
  • a thread
 
Question 5)
Which choice is not a scalar data type?
  • tuple
  • integer
  • float
  • boolean
 
Question 6)
 _________ cannot be destructured.
  • Tuples
  • Enums
  • Traits
  • Structs
 
Question 7)
Which cargo command checks a program for error without creating a
binary executable?
  • cargo build
  • cargo check
  • cargo –version
  • cargo init
Question 8)
Which is valid syntax for defining an array of i32 values?
  • Array::new(10)
  • [i32; 10] 
  • Array::with_capacity(10)
  • [i32]
 
Question 9)
What is an alternative way of writing slice that produces the same
result?
let s = String::form(“hello”);
let slice = &s[0..2];
  • let slice = &s[len – 2];
  • let slice = &s.copy(0..2);
  • let slice = &s[..2];
  • let slice = &s[len + 2];
 
Question 10)
Using the ? operator at the end of an expression is equivalent to
_______.
  • calling ok_error()
  • calling panic!()
  • a match pattern that branches into True or False
  • a match pattern that may result an early return
 
Question 11)
What syntax is required to take a mutable reference to T, when used
within a function argument?
 
fn increment(i: T) {
    // body elided
}
 
  • *mut T
  • mut &T
  • &mut T
  • mut ref T
 
Question 12)
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++?
  • !
  • ()
  • None
  • Null
 
Question 13)
To convert a Result to an Option, which method should you use?
  • .ok()
  • .to_option()
  • .into()
  • .as_option()
 
Question 14)
What happens when an error occurs that is being handled by the question
mark (?) operator?
  • The program panics immediately.
  • The error is reported and execution continues.
  • An exception is raised. The effect(s) of the exception are defined by
    the error! macro.
  • 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.

 
Question 15)
Which comment syntax is not legal?
  •  /*
  •  //
  •  # 
  •  //!
Question 16)
Defining a ______ requires a lifetime parameter.
  • function with a generic argument
  • function that ends the lifetime of one of its arguments
  • struct that contains a reference to a boxed value
  • struct that contains a reference to a value
 
 
Question 17)
In matching patterns, values are ignored with __________.
  •  ..  
  •  skip
  •  .ignore()
  •  an underscore (_)

 

Question 18)
Which example correctly uses std::collections::HashMap’s Entry API to
populate counts?
 
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);
}
for c in text.chars() {
    if let Some(count) = &mut counts.get(&c) {
        counts.insert(c, *count + 1);
    } else {
        counts.insert(c, 1);
    };
}
 
 
for c in text.chars() {
    let count = counts.entry(c).or_insert(0);
    *count += 1;
}
for c in text.chars() {
    let count = counts.entry(c);
    *count += 1;
}
for c in text.chars() {
    counts.entry(c).or_insert(0).map(|x| x + 1);
}


Question 19)
Which fragment does not incur memory allocations while writing to a
“file” (represented by a Vec)?
 
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(())
}
write!(&mut v, “{}{}{}”, a, b, c)?;
[ ]
v.write(a)?;
v.write(b)?;
v.write(c)?;
 
[ ]
v.write(a, b, c)?;
 
[ ]
v.write_all(a.as_bytes())?;
v.write_all(&b.to_string().as_bytes())?;
c.encode_utf8(&mut v);
Answered in rust user forum
 
Question 20)
Does the main function compile? If so, why? If not, what do you need to
change?
 
fn main() {
    let Some(x) = some_option_value;
}
 
  • The code compiles. let do not require a refutable pattern.
  • The code does not compile. let statements require a refutable
    pattern. Add if before let.
  • The code compiles. let statements sometimes require a refutable
    pattern.
  • The code does not compile. let statements requires an irrefutable
    pattern. Add if before let.
Question 21)
Which types are not allowed within an enum variant’s body?
  • structs
  • trait objects
  • zero-sized types
  • floating-point numbers
 
Question 22)
Which statement about this code is true?
 
fn main() {
    let c = 'z';
    let heart_eyed_cat = '😻';
}
  • heart_eyed_cat is an invalid expression.
  • Both are character literals.
  • c is a string literal and heart_eyed_cat is a character
    literal.
  • Both are string literals.

 

Question 23)
Which statement about lifetimes is false?
  • Lifetimes are specified when certain values must outlive
    others.
  • Lifetimes are always inferred by the compiler.
  • Lifetimes were redundantly specified in previous version of
    Rust.
  • Lifetimes are specified when a struct is holding a reference to a
    value.
Question 24)
Which statement about the Clone and Copy traits is false?
  • Copy is enabled for primitive, built-in types.
  • When using Clone, copying data is explicit.
  • Without Copy, Rust applies move semantics to a type’s access.
  • Until a type implements either Copy or Clone, its internal data
    cannot be copied.
 
Question 25)
Why does this code not compile?
 
fn returns_closure() -> dyn Fn(i32) -> i32 {
    |x| x + 1
}
 
  • Closures are types and can be returned only if the concrete trait
    is implemented.
  • Closures are represented by traits, so they cannot be a return
    type.
  • The returned fn pointer and value need to be represented by another
    trait.
  • Closures are types, so they cannot be returned directly from a
    function.
Question 26)
What smart pointer is used to allow multiple ownership of a value in
various threads?
  • Arc<T>
  • Rc<T>
  • Box<T>
  • Both Arc<T> and Rc<T> are multithread safe.
Question 27)
In this function. what level of access is provided to the variable
a?

use std::fmt::Debug;
fn report<T:Debug>(a: &T) {
    eprintln!(“info: {:?}”, a);
}
 
  • print
  • debug
  • read/write
  • read-only
Question 28)
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?
  • Arc<Mutex<T>>
  • Mutex<Arc<T>>
  • Rc<Mutex<T>>
  • Mutex<Rc<T>>
  • Rust book reference
 
 
Question 29)
Which idiom can be used to concatenate the strings a, b, c?
let a = “a”.to_string();
let b = “b”.to_string();
let c = “c”.to_string();
 
  • concat(a,b,c)
  • a + b + c
  • String::from(a,b,c)
  • format!(“{}{}{}”, a, b, c)
Question 30)
Which statement about enums is false?
  • Option is an enum type.
  • Enums are useful in matching patterns.
  • Enum variants can have different types with associated data.
  • the term enum is short for enummap
 
 
Question 31)
What does an underscore (_) indicate when used as pattern?
  • It matches nothing.
  • It matches underscores.
  • It matches everything.
  • It matches any value that has a length of 1.
Question 32)
Which choice is not valid loop syntax?
  • while
  • do 
  • loop
  • for
 
Question 33)
How do you construct a value of Status that is initialized to
Waiting?

enum Status {
    Waiting,
    Busy,
    Error(String),
}
 
  • let s = Status::Waiting;
  • let s = Status::new(Waiting);
  • let s = Enum::new(Status::Waiting);
  • let s = new Status::Waiting;
Question 34)
Generics are useful when you ________.
  • need to reduce code duplication by abstracting values further,
    such as in function parameters
  • need to reduce code duplication by concretizing values and
    restricting parameters in functions
  • need a supertrait
  • are not sure if you need a specific kind of trait
 
Question 35)
How do you create a Rust project on the command-line?
  • rustup init
  • cargo start
  • cargo new
  • rust new-project
Question 36)
What is a safe operation on a std::cell:UnsafeCell<T>?
  • The only safe operation is the .get() method, which returns only a
    raw pointer.
  • UnsafeCell<T> provides thread-safety. Therefore, creating
    &T references from multiple threads is safe.
  • Non. UnsafeCell<T> only allows code that would otherwise need
    unsafe blocks to be written in safe code.
  • A &mut T reference is allowed. However it may not cpexists with
    any other references. and may be created only in single-threaded
    code.

Leave a Comment