Practice 50 Variables and Data Types Coding Questions, TechnoVlogs

Practice 50 Variables and Data Types Coding Questions


Q1. Write a Rust program to declare and print the value of an integer variable.  
Input:  
None  
Expected Output:  
Value of the integer variable: 42

Q2. Write a Rust program to swap the values of two variables.  
Input:  
a = 5, b = 10  
Expected Output:  
After swapping: a = 10, b = 5

Q3. Write a Rust program to calculate the area of a rectangle using variables.  
Input:  
Length: 5, Width: 3  
Expected Output:  
Area: 15

Q4. Write a Rust program to demonstrate the use of a mutable variable.  
Input:  
Initial value: 10, Updated value: 20  
Expected Output:  
Updated value of the variable: 20

Q5. Write a Rust program to declare and print a floating-point variable.  
Input:  
None  
Expected Output:  
Value of the floating-point variable: 3.14

Q6. Write a Rust program to check the default value of an uninitialized variable.  
Input:  
None  
Expected Output:  
Error: variable not initialized

Q7. Write a Rust program to demonstrate shadowing of variables.  
Input:  
None  
Expected Output:  
Shadowed value: 10

Q8. Write a Rust program to perform addition using integer variables.  
Input:  
a = 7, b = 3  
Expected Output:  
Sum: 10

Q9. Write a Rust program to declare a constant and print its value.  
Input:  
None  
Expected Output:  
Value of the constant: 100

Q10. Write a Rust program to demonstrate type inference in variable declaration.  
Input:  
None  
Expected Output:  
Inferred type: integer, Value: 42

Q11. Write a Rust program to assign a string to a variable and print it.  
Input:  
String: "Hello, Rust!"  
Expected Output:  
Value of the string variable: Hello, Rust!

Q12. Write a Rust program to find the size of a variable using `std::mem::size_of`.  
Input:  
Variable: i32  
Expected Output:  
Size of i32: 4 bytes

Q13. Write a Rust program to perform division using floating-point variables.  
Input:  
a = 9.0, b = 2.0  
Expected Output:  
Quotient: 4.5

Q14. Write a Rust program to demonstrate boolean variables.  
Input:  
value = true  
Expected Output:  
Boolean value: true

Q15. Write a Rust program to demonstrate character variables.  
Input:  
Character: 'R'  
Expected Output:  
Value of the character variable: R

Q16. Write a Rust program to calculate the sum of two variables of type `u32`.  
Input:  
a = 15, b = 25  
Expected Output:  
Sum: 40

Q17. Write a Rust program to demonstrate the immutability of variables.  
Input:  
Initial value: 5, Attempted update: 10  
Expected Output:  
Error: cannot assign twice to an immutable variable

Q18. Write a Rust program to assign a tuple to a variable and access its elements.  
Input:  
Tuple: (10, 20, 30)  
Expected Output:  
First element: 10, Second element: 20, Third element: 30

Q19. Write a Rust program to declare and print an array variable.  
Input:  
Array: [1, 2, 3, 4, 5]  
Expected Output:  
Array elements: [1, 2, 3, 4, 5]

Q20. Write a Rust program to find the length of a string variable.  
Input:  
String: "Rustacean"  
Expected Output:  
Length of the string: 9

Q21. Write a Rust program to demonstrate integer overflow.  
Input:  
Variable: u8, Value: 256  
Expected Output:  
Error: attempt to add with overflow

Q22. Write a Rust program to demonstrate slicing with an array variable.  
Input:  
Array: [10, 20, 30, 40, 50], Slice: [1..3]  
Expected Output:  
Sliced elements: [20, 30]

Q23. Write a Rust program to perform arithmetic operations using different numeric types.  
Input:  
a = 10 (i32), b = 3.5 (f32)  
Expected Output:  
Sum: 13.5

Q24. Write a Rust program to assign a boolean variable based on a comparison.  
Input:  
a = 5, b = 10  
Expected Output:  
Comparison result: false

Q25. Write a Rust program to assign a multi-line string to a variable and print it.  
Input:  
String: "Rust is\nfast and safe."  
Expected Output:  
Rust is  
fast and safe.

Q26. Write a Rust program to declare and print a tuple containing different data types.  
Input:  
Tuple: (42, true, "Rust")  
Expected Output:  
Tuple: (42, true, "Rust")

Q27. Write a Rust program to demonstrate the use of the `String` type.  
Input:  
String: "Learning Rust"  
Expected Output:  
String value: Learning Rust

Q28. Write a Rust program to convert an integer to a floating-point variable.  
Input:  
Integer: 42  
Expected Output:  
Floating-point value: 42.0

Q29. Write a Rust program to perform a mathematical operation on a mutable variable.  
Input:  
Initial value: 5, Increment: 3  
Expected Output:  
Updated value: 8

Q30. Write a Rust program to demonstrate the use of `Option` variables.  
Input:  
Variable: Some(10)  
Expected Output:  
Value: 10

Q31. Write a Rust program to check if a `String` variable is empty.  
Input:  
String: ""  
Expected Output:  
The string is empty

Q32. Write a Rust program to assign a range to a variable and iterate over it.  
Input:  
Range: 1..5  
Expected Output:  
1, 2, 3, 4

Q33. Write a Rust program to assign a vector to a variable and print its elements.  
Input:  
Vector: [1, 2, 3]  
Expected Output:  
Vector elements: [1, 2, 3]

Q34. Write a Rust program to demonstrate string concatenation.  
Input:  
String1: "Hello", String2: "World"  
Expected Output:  
Concatenated string: HelloWorld

Q35. Write a Rust program to assign a boolean value based on logical operators.  
Input:  
a = true, b = false  
Expected Output:  
Logical AND: false, Logical OR: true

Q36. Write a Rust program to demonstrate destructuring assignment with tuples.  
Input:  
Tuple: (1, "Rust", 3.5)  
Expected Output:  
First: 1, Second: Rust, Third: 3.5

Q37. Write a Rust program to demonstrate formatting of floating-point variables.  
Input:  
Value: 3.14159  
Expected Output:  
Formatted value: 3.14

Q38. Write a Rust program to calculate the length of an array using the `.len()` method.  
Input:  
Array: [10, 20, 30, 40]  
Expected Output:  
Length of the array: 4

Q39. Write a Rust program to create a mutable string and update its value.  
Input:  
Initial: "Rust", Append: "Lang"  
Expected Output:  
Updated string: RustLang

Q40. Write a Rust program to convert a string slice to an owned `String`.  
Input:  
Slice: "Hello"  
Expected Output:  
Owned string: Hello

Q41. Write a Rust program to assign and print a large integer using the `i64` type.  
Input:  
Value: 9876543210  
Expected Output:  
Large integer: 9876543210

Q42. Write a Rust program to demonstrate variable shadowing with a new data type.  
Input:  
Initial: integer (10), Shadowed: floating-point (10.5)  
Expected Output:  
Shadowed value: 10.5

Q43. Write a Rust program to convert a string to an integer.  
Input:  
String: "42"  
Expected Output:  
Integer value: 42

Q44. Write a Rust program to check if a vector is empty.  
Input:  
Vector: []  
Expected Output:  
The vector is empty

Q45. Write a Rust program to demonstrate basic type conversion using `as`.  
Input:  
Value: 5 (i32)  
Expected Output:  
Converted value: 5.0 (f32)

Q46. Write a Rust program to declare a struct and initialize it with values.  
Input:  
Struct: Point { x: 5, y: 10 }  
Expected Output:  
Point coordinates: x = 5, y = 10

Q47. Write a Rust program to split a string into substrings using whitespace.  
Input:  
String: "Rust is awesome"  
Expected Output:  
Substrings: ["Rust", "is", "awesome"]

Q48. Write a Rust program to check the type of a variable using debug formatting.  
Input:  
Variable: 42  
Expected Output:  
Type of variable: i32

Q49. Write a Rust program to demonstrate the use of underscores in numeric literals.  
Input:  
Value: 1_000_000  
Expected Output:  
Value: 1000000

Q50. Write a Rust program to assign a raw string literal to a variable.  
Input:  
String: r#"This is a "raw" string."#  
Expected Output:  
Raw string: This is a "raw" string.

Share on Social Media