Zig Interview Questions Hindi mein

Zig कोर्स के 25 सवाल, हर सवाल के सही जवाब और explanation के साथ। जवाब देखने से पहले ख़ुद सोचें।

Getting Started with Zig

1. Which is a core Zig design principle?

  1. No hidden control flow and no hidden allocations
  2. Hidden memory allocations for convenience
  3. Exceptions for all errors
  4. Garbage collection
जवाब देखें

जवाब: No hidden control flow and no hidden allocations

Zig makes control flow, allocation and errors explicit.

पाठ: What Zig Is and Why It Exists

2. Which build mode keeps runtime safety checks while optimising?

  1. Debug
  2. ReleaseFast
  3. ReleaseSmall
  4. ReleaseSafe
जवाब देखें

जवाब: ReleaseSafe

ReleaseSafe optimises but keeps bounds and overflow checks.

पाठ: Installing Zig and Using the Toolchain

3. How do you convert a u32 to a u8 in Zig?

  1. It happens implicitly
  2. With a C-style cast (u8)x
  3. With @intCast, which is checked at run time in safe build modes
  4. It is impossible
जवाब देखें

जवाब: With @intCast, which is checked at run time in safe build modes

Narrowing conversions require @intCast, which panics on out-of-range values in safe modes.

पाठ: Variables, Types and Integers

Control Flow, Arrays, Slices and Pointers

4. What does `for (items, 0..) |item, i|` do?

  1. Iterates items twice
  2. Declares a range of zeros
  3. Iterates items together with an index starting at 0
  4. It is invalid syntax
जवाब देखें

जवाब: Iterates items together with an index starting at 0

Multi-object for loops iterate several sequences in lockstep, here items and an index range.

पाठ: if, while, for, switch and Labelled Blocks

5. How should you compare the contents of two strings in Zig?

  1. With ==
  2. With std.mem.eql(u8, a, b)
  3. With strcmp
  4. They cannot be compared
जवाब देखें

जवाब: With std.mem.eql(u8, a, b)

Slices are pointer and length; std.mem.eql compares their bytes.

पाठ: Arrays, Slices and Strings

6. How do you represent a pointer that may be null in Zig?

  1. With an optional pointer ?*T, which must be unwrapped before use
  2. *T can always be null
  3. With [*]T
  4. With undefined
जवाब देखें

जवाब: With an optional pointer ?*T, which must be unwrapped before use

Plain pointers are non-null; ?*T makes nullability explicit and checked.

पाठ: Pointers and Optional Pointers

Structs, Unions and Error Handling

7. What does `inv.markPaid()` desugar to when markPaid takes `self: *Invoice`?

  1. Invoice.markPaid(&inv), passing a pointer
  2. Invoice.markPaid(inv) with a copy
  3. A virtual method call
  4. It is a compile error
जवाब देखें

जवाब: Invoice.markPaid(&inv), passing a pointer

Method call syntax passes the address automatically when the parameter is a pointer.

पाठ: Structs, Methods and Namespaces

8. What happens when you add a new variant to a tagged union that is switched on without an else prong?

  1. Nothing
  2. It is handled by the first prong
  3. It panics at run time
  4. Every non-exhaustive switch becomes a compile error until it handles the new case
जवाब देखें

जवाब: Every non-exhaustive switch becomes a compile error until it handles the new case

Exhaustive switches make the compiler point to every place to update.

पाठ: Enums, Tagged Unions and Exhaustive switch

9. What does `try foo()` do if foo returns an error?

  1. Ignores the error
  2. Panics
  3. Returns that error from the current function immediately
  4. Retries foo
जवाब देखें

जवाब: Returns that error from the current function immediately

try is shorthand for catching the error and returning it.

पाठ: Errors, Optionals, try, catch and errdefer

Memory and Allocators

10. Why do Zig functions that allocate take an Allocator parameter?

  1. Because Zig has a garbage collector
  2. For faster compilation
  3. So callers control and can see the memory strategy, and allocation is never hidden
  4. Because global variables are forbidden
जवाब देखें

जवाब: So callers control and can see the memory strategy, and allocation is never hidden

Explicit allocators make memory behaviour visible and swappable.

पाठ: Allocators: Explicit Memory Management

11. When does an errdefer statement run?

  1. Always at scope exit
  2. Only when the scope exits by returning an error
  3. Only on success
  4. At program start
जवाब देखें

जवाब: Only when the scope exits by returning an error

errdefer is for cleanup that must happen only on the error path.

पाठ: defer, errdefer and Ownership

12. In Zig 0.15, how do you append to an unmanaged std.ArrayList?

  1. try list.append(allocator, item)
  2. list.append(item)
  3. list.push(item)
  4. ArrayLists cannot grow
जवाब देखें

जवाब: try list.append(allocator, item)

Unmanaged containers take the allocator as an argument to allocating methods.

पाठ: ArrayList, HashMap and Other Containers

Compile-Time Programming

13. What can a function do with a `comptime T: type` parameter?

  1. Use T as a type in its body, making the function generic over T
  2. Nothing; types cannot be parameters
  3. Change T at run time
  4. Only print T
जवाब देखें

जवाब: Use T as a type in its body, making the function generic over T

Types are compile-time values, which is how Zig does generics.

पाठ: comptime Basics

14. How are generic data structures usually written in Zig?

  1. With a template keyword
  2. With macros
  3. With inheritance
  4. As functions that take comptime parameters and return a type
जवाब देखें

जवाब: As functions that take comptime parameters and return a type

For example, fn Stack(comptime T: type) type { return struct { ... }; }.

पाठ: Generic Types and Functions

15. What does @field(value, name) do when name is comptime-known?

  1. Creates a new field at run time
  2. Deletes a field
  3. Accesses the field of value with that name, resolved at compile time
  4. Returns the field's type only
जवाब देखें

जवाब: Accesses the field of value with that name, resolved at compile time

@field enables reflection-driven access with no run-time lookup.

पाठ: Reflection with @typeInfo and anytype

Safety, C Interop and Cross-Compilation

16. What does `x +|= 10` do for an unsigned integer near its maximum?

  1. Wraps around to a small number
  2. Panics
  3. Saturates at the maximum value of the type
  4. Converts to a larger type
जवाब देखें

जवाब: Saturates at the maximum value of the type

Saturating operators clamp to the type's range.

पाठ: Integer Semantics, Safety Checks and Undefined Behaviour

17. What does `export fn` do in Zig?

  1. Makes a function public within the file
  2. Makes the function callable from C with the C ABI and an unmangled name
  3. Runs the function at compile time
  4. Exports it to JavaScript only
जवाब देखें

जवाब: Makes the function callable from C with the C ABI and an unmangled name

export gives a Zig function C linkage so other languages can call it.

पाठ: Interoperating with C

18. Why can Zig cross-compile C code without installing a separate cross toolchain for common targets?

  1. It bundles libc headers and sources and builds what the target needs
  2. It emulates the target
  3. It only supports Linux
  4. It downloads Docker images
जवाब देखें

जवाब: It bundles libc headers and sources and builds what the target needs

Zig ships libc implementations and headers for many targets.

पाठ: Cross-Compilation and zig cc

The Build System, Packages and Testing

19. What is build.zig?

  1. A Zig program that declares the build graph using std.Build
  2. A configuration file in YAML
  3. A Makefile
  4. A list of compiler flags
जवाब देखें

जवाब: A Zig program that declares the build graph using std.Build

Builds are written in Zig itself, so they are portable and programmable.

पाठ: build.zig: Builds as Zig Code

20. What does the hash in a build.zig.zon dependency guarantee?

  1. Faster downloads
  2. That the package is popular
  3. That it was reviewed by the Zig team
  4. That the fetched content is exactly what was recorded, making builds reproducible
जवाब देखें

जवाब: That the fetched content is exactly what was recorded, making builds reproducible

Content hashes detect any change in the downloaded package.

पाठ: Packages and Dependencies with build.zig.zon

21. What happens if a test using std.testing.allocator leaks memory?

  1. Nothing
  2. The program crashes in production
  3. The test fails and reports the leak
  4. Zig frees it silently
जवाब देखें

जवाब: The test fails and reports the leak

The testing allocator tracks allocations and fails leaking tests.

पाठ: Testing in Zig

Concurrency, Performance, Idioms and Revision

22. What is the status of async/await in current Zig?

  1. It is stable and widely used
  2. It was always unsupported
  3. Language-level async was removed; a new explicit Io-based design is in development
  4. It is required for all I/O
जवाब देखें

जवाब: Language-level async was removed; a new explicit Io-based design is in development

Async keywords were removed during the self-hosted compiler transition.

पाठ: Threads, Atomics and Synchronisation

23. Why must you call flush on a buffered writer in Zig 0.15?

  1. To free memory
  2. Because buffered data is only written when the buffer fills or flush is called
  3. To close the file
  4. It is optional and does nothing
जवाब देखें

जवाब: Because buffered data is only written when the buffer fills or flush is called

Unflushed data stays in the buffer and can be lost when the program exits.

पाठ: Low-Level Performance: Layout, SIMD and Buffered I/O

24. Why is returning a slice of a local array from a function a bug?

  1. The array lives on the function's stack frame, so the slice dangles after return
  2. Slices cannot be returned
  3. It is slow
  4. Arrays are always heap-allocated
जवाब देखें

जवाब: The array lives on the function's stack frame, so the slice dangles after return

Stack memory is reclaimed when the function returns.

पाठ: Idiomatic Zig and Common Pitfalls

25. Which Zig keyword runs cleanup only when a function returns an error?

  1. defer
  2. catch
  3. unreachable
  4. errdefer
जवाब देखें

जवाब: errdefer

errdefer is the tool for cleaning up partial work on failure.

पाठ: Revision and Interview Questions

← कोर्स पर वापस जाएँ