Practice 50 Go Lang Error Handling Coding Questions, TechnoVlogs

Practice 50 Go Lang Error Handling Coding Questions


Q1. Write a Go program that takes a number as input and checks if it is greater than 10. If not, return an error.  
  Input: 
  5    
  Expected Output:  
  Error: Number must be greater than 10

Q2. Write a Go program to divide two numbers. Handle the division by zero error.  
  Input:    
  10, 0
  Expected Output:  
  Error: Division by zero

Q3. Write a Go program to read a number from the user and check if it is a valid integer.  
  Input:    
  12.5
  Expected Output:  
  Error: Invalid input. Please enter an integer.

Q4. Write a Go program to open a file. Handle errors if the file does not exist.  
  Input:    
  nonexistent_file.txt
  Expected Output:  
  Error: File not found

Q5. Write a Go program to check if a string is empty and return an error if it is.  
  Input:    
  ""
  Expected Output:  
  Error: String is empty

Q6. Write a Go program to check if a user has access to a resource. Return an error if access is denied.  
  Input:    
  user: guest
  Expected Output:  
  Error: Access denied for guest

Q7. Write a Go program that takes a number and checks if it is positive. If not, return an error.  
  Input:    
  -5
  Expected Output:  
  Error: Number must be positive

Q8. Write a Go program that reads a file and handles the error if the file cannot be read.  
  Input:    
  file.txt (which does not exist)
  Expected Output:  
  Error: Unable to read the file

Q9. Write a Go program to perform subtraction of two numbers and return an error if the result is negative.  
  Input:    
  5 - 8
  Expected Output:  
  Error: Result is negative

Q10. Write a Go program that attempts to connect to a database and returns an error if the connection fails.  
   Input:     
   Database: example_database
   Expected Output:  
   Error: Failed to connect to the database

Q11. Write a Go program to check if a user is logged in. Return an error if the user is not logged in.  
   Input:     
   User: anonymous
   Expected Output:  
   Error: User is not logged in

Q12. Write a Go program to validate email input. Return an error if the email is not in the correct format.  
   Input:     
   "user@domain"
   Expected Output:  
   Error: Invalid email format

Q13. Write a Go program that attempts to open a file and return an error if the file path is invalid.  
   Input:     
   "invalid_path.txt"
   Expected Output:  
   Error: Invalid file path

Q14. Write a Go program that accepts a number and checks if it is between 1 and 100. Return an error if it is outside that range.  
   Input:     
   150
   Expected Output:  
   Error: Number must be between 1 and 100

Q15. Write a Go program that attempts to open a URL and returns an error if the URL is invalid.  
   Input:     
   "htp://example.com"
   Expected Output:  
   Error: Invalid URL format

Q16. Write a Go program that attempts to delete a file and returns an error if the file does not exist.  
   Input:     
   "nonexistent_file.txt"
   Expected Output:  
   Error: File does not exist

Q17. Write a Go program to check if an array index is valid. Return an error if the index is out of bounds.  
   Input:     
   Array: [1, 2, 3], Index: 5
   Expected Output:  
   Error: Index out of bounds

Q18. Write a Go program to check if a number is even. Return an error if it is odd.  
   Input:     
   7
   Expected Output:  
   Error: Number is odd

Q19. Write a Go program that checks if a user has permission to delete a file. Return an error if the user does not have permission.  
   Input:     
   User: "guest"
   File: "confidential_file.txt"
   Expected Output:  
   Error: Permission denied to delete the file

Q20. Write a Go program to check if a number is divisible by 5. Return an error if it is not.  
   Input:     
   11
   Expected Output:  
   Error: Number is not divisible by 5

Q21. Write a Go program that tries to parse a string into an integer. Return an error if parsing fails.  
   Input:     
   "abc"
   Expected Output:  
   Error: Invalid integer input

Q22. Write a Go program that reads an integer from the user and checks if it is positive. Return an error if it is not.  
   Input:     
   -3
   Expected Output:  
   Error: Number must be positive

Q23. Write a Go program that checks if a password meets certain criteria (length and characters). Return an error if it doesn't.  
   Input:     
   "short"
   Expected Output:  
   Error: Password must be at least 8 characters long

Q24. Write a Go program that checks if a number is divisible by both 3 and 5. Return an error if it is not.  
   Input:     
   7
   Expected Output:  
   Error: Number is not divisible by 3 and 5

Q25. Write a Go program that checks if a file can be written to. Return an error if the file is read-only.  
   Input:     
   "readonly_file.txt"
   Expected Output:  
   Error: File is read-only

Q26. Write a Go program that takes user input for age and checks if the age is valid (positive number). Return an error if the age is invalid.  
   Input:     
   -5
   Expected Output:  
   Error: Invalid age

Q27. Write a Go program that checks if a given file has read permissions. Return an error if it does not.  
   Input:     
   "file.txt"
   Expected Output:  
   Error: No read permissions for file.txt

Q28. Write a Go program that checks if a number is prime. Return an error if the number is not prime.  
   Input:     
   4
   Expected Output: 
   Error: 4 is not a prime number

Q29. Write a Go program that takes an input number and checks if it's within a specific range (1-50). Return an error if it is not.  
   Input:     
   60
   Expected Output:  
   Error: Number out of range (1-50)

Q30. Write a Go program that attempts to connect to a server. Return an error if the connection fails.  
   Input:     
   Server: "example.com"
   Expected Output:  
   Error: Failed to connect to the server

Q31. Write a Go program to check if a string contains a specific substring. Return an error if the substring is not found.  
   Input:     
   String: "hello world", Substring: "abc"
   Expected Output:  
   Error: Substring not found

Q32. Write a Go program that attempts to read a file. Return an error if the file is empty.  
   Input:     
   File: "empty_file.txt"
   Expected Output:  
   Error: File is empty

Q33. Write a Go program to handle errors when opening a file for writing. Return an error if the file cannot be opened.  
   Input:    
   File: "readonly_file.txt"
   Expected Output:  
   Error: Cannot open file for writing

Q34. Write a Go program to check if a directory exists. Return an error if the directory is not found.  
   Input:     
   Directory: "my_directory"
   Expected Output:  
   Error: Directory not found

Q35. Write a Go program to handle an error when parsing JSON data. Return an error if the JSON is invalid.  
   Input:     
   JSON: {"name": "John", age: 30}
   Expected Output:  
   Error: Invalid JSON format

Q36. Write a Go program to check if a number is divisible by 2. Return an error if it is not.  
   Input:     
   9
   Expected Output:  
   Error: Number is not divisible by 2

Q37. Write a Go program to check if a given number is within an acceptable limit (e.g., 1 to 100). Return an error if the number is out of bounds.  
   Input:     
   150
   Expected Output:  
   Error: Number out of bounds

Q38. Write a Go program that checks if a username is already taken. Return an error if the username is taken.  
   Input:     
   Username: "admin"
   Expected Output:  
   Error: Username "admin" is already taken

Q39. Write a Go program that takes a string input and checks if it contains any digits. Return an error if it does.  
   Input:     
   String: "abc123"
   Expected Output:  
   Error: String contains digits

Q40. Write a Go program to check if the input number is within the range of 0 to 100. Return an error if the number is out of range.  
   Input:     
   150
   Expected Output:  
   Error: Number out of range

Q41. Write a Go program to check if a file is empty. Return an error if it is empty.  
   Input:     
   "empty_file.txt"
   Expected Output:  
   Error: File is empty

Q42. Write a Go program to check if a file path is valid. Return an error if the path is invalid.  
   Input:     
   "invalid_path"
   Expected Output:  
   Error: Invalid file path

Q43. Write a Go program to check if a user is eligible for voting. Return an error if the user is under 18.  
   Input:     
   Age: 16
   Expected Output:  
   Error: User is not eligible to vote

Q44. Write a Go program to check if an input string has at least 5 characters. Return an error if it does not.  
   Input:     
   "abc"
   Expected Output:  
   Error: String must have at least 5 characters

Q45. Write a Go program that attempts to open a file. Return an error if the file cannot be opened.  
   Input:     
   "file.txt" (which does not exist)
   Expected Output:  
   Error: Unable to open file.txt

Q46. Write a Go program to check if a given year is a leap year. Return an error if it is not a leap year.  
   Input:     
   2021
   Expected Output:  
   Error: 2021 is not a leap year

Q47. Write a Go program that checks if a number is an even number. Return an error if it is not even.  
   Input:     
   15
   Expected Output:  
   Error: Number is not even

Q48. Write a Go program that checks if a given integer is a valid positive number. Return an error if it is negative.  
   Input:    
   -10
   Expected Output:  
   Error: Invalid input. Number must be positive.

Q49. Write a Go program to read a string from the user and check if it contains any whitespace. Return an error if it does.  
   Input:     
   "hello world"
   Expected Output:  
   Error: String contains whitespace

Q50. Write a Go program that checks if a user has the required permissions to access a resource. Return an error if permissions are denied.  
   Input:     
   User: "guest", Resource: "confidential_file.txt"
   Expected Output:  
   Error: Insufficient permissions to access confidential_file.txt

Share on Social Media