linkedin-skill-assessments-quizzes

Rust (Programming Language)

Q1. Which type cast preserves the mathematical value in all cases?

Q2. What do the vertical bars represent here?

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

reference

Q3. Which choice is not a scalar data type?

Q4. _ cannot be destructured.

reference

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

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

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

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

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

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

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

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

reference

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

Q14. In matching patterns, values are ignored with _.

Q15. Defining a _ requires a lifetime parameter.

Rust book reference

Q16. 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);
}

reference

Q17. 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);
  1. Answered in rust user forum
  2. reference

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

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

Q19. Which statement about lifetimes is false?

Q20. 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++?

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

Q22. Which statement about the Clone and Copy traits is false?

ref from stack overflow

Q23. Why does this code not compile?

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

Rust book reference

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

Rust book reference

Q25. Which types are not allowed within an enum variant’s body?

Reference

Q26. Which statement about this code is true?

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

Reference

Q27. 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?

Rust book reference

Q28. 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();

Q29. 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);
}

Q30. Which choice is not valid loop syntax?

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

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

Q32. Which statement about enums is false?

Q33. What does an underscore (_) indicate when used as pattern?

Q34. What is a safe operation on a std::cell:UnsafeCell<T>?

Reference

Q35. Generics are useful when you _.

Q36. _ cannot be destructed.

Q37. How do you create a Rust project on the command-line?