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. How do you create a Rust project on the command-line?

Q37. Calling.clone() _.

Reference

Q38. what is one of the roles of the let keyword?

let text = String::new("LinkedIn");

Reference

Q39. How is a new enum initialized?

enum Option_i32 {
    Some(i32),
    None,
}

Reference

Q40. What are the main difference between const and static?

Reference

Q41. Which Rust data type represents a signed integer that has the same width as a pointer of the compile target’s CPU?

Reference

Q42. When are supertraits needed?

Reference

if x {
    println!("ok");
}

Reference

Q44. How do you access the married data in this struct?

struct person = Person {
    height: u64,
    weight: u64,
    married: bool
}

Reference

Q45. To mark a function as visible to other crates, what do you need to do to its definition?

Reference

Q46. Which choice is a compound data type?

Reference

Q47. How could you make this function compile?

fn main() {
    let x = 5;
    println!("The value of x is: {}", x);
    x = 6;
    println!("The value of x is: {}", x);
}

Reference

Q48. Using .unwrap() will _.

Reference

Q49. When should the panic! macro be called instead of using std::result::Result?

Reference

Q50. Which statement about arrays is true?

Reference

Q51. How would you select the value 2.0 from this tuple?

let pt = Point2D(-1.0, 2.0)

Reference

Q52. When writing tests, which macro should you use to assert equality between two values?

Reference

Q53. Which code statement in Rust is used to define a BTreeMap object?

Reference

Q54 .Rust is known to be memory safe. Which feature is the main reason for the memory safety of Rust?

Reference

Q55 . To support Dynamic Sized variables, what should we use in place of “f32”?

Reference

Q56 . What is “Drop” in Rust used for?

Reference

Q57 . In Rust, how is a macro from the above Rust code snippet used?

Reference

Q58 . Which library does Rust use for memory allocation?

Reference

Q59 . Who designed Rust from scratch in 2006?

Reference

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

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

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

Q63. Which statement about this code is true?

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

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

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

Q65. How would you select the value 2.0 from this tuple?

let pt = Point2D(-1.0, 2.0)

Q66. What is the purpose of the move keyword in Rust?

reference