Laravel and PHP Based Interview Questions That Mostly Asked

 Laravel and PHP Based Interview Questions That Mostly Asked


1. Union and Union All:

   - Union: Combines the result sets of two or more SELECT queries and removes duplicates.

   - Union All: Combines the result sets of two or more SELECT queries without removing duplicates.




2. Git Rename:

   - To rename a file in Git, use the command: `git mv old_filename new_filename`. This stages the change, and you need to commit it with `git commit`.




3. What is .env? How to Protect?:

   - .env: A file used to store environment variables in a key-value format. It is typically used to store sensitive information like API keys, database credentials, etc.

   - Protection: Ensure the `.env` file is listed in `.gitignore` so it is not committed to version control. You can also set file permissions to restrict access.




4. What is .gitignore?:

   - A `.gitignore` file specifies which files or directories Git should ignore and not track. Common entries include sensitive files, build outputs, and temporary files.




5. What is package.json?:

   - A `package.json` file is used in Node.js projects to manage project dependencies, scripts, and metadata. It defines the packages your project depends on and the scripts to run tasks.




6. What is composer.json?:

   - `composer.json` is a file used in PHP projects to manage dependencies via Composer. It defines the libraries your project needs and their versions.




7. Laravel Lifecycle:

   - The Laravel lifecycle involves the following steps:

     1. Request: A user sends a request to the application.

     2. Service Providers: Laravel loads the necessary service providers.

     3. Routing: The request is routed to the appropriate controller or closure.

     4. Controller: The controller processes the request.

     5. Response: The controller returns a response, which is sent back to the user.




8. Laravel Route Binding:

   - Implicit Binding: Automatically resolves Eloquent models based on route parameters.

   - Explicit Binding: Manually binds a model to a route parameter using `Route::model()` or `Route::bind()`.




9. Laravel Controller Binding:

   - Binding a controller method to a route using route declarations like `Route::get('/users/{id}', [UserController::class, 'show']);`.




10. Service Provider & Container:

    - Service Providers: Central place to register services, bindings, and configurations.

    - Container: Manages class dependencies and performs dependency injection.




11. PostgreSQL:

    - An open-source relational database management system (RDBMS) known for its extensibility and standards compliance.




12. Indexing:

    - A database technique used to speed up the retrieval of data from a table by creating a data structure (index) that provides quick access to rows.




13. How to Manage Large Databases:

    - Strategies include indexing, partitioning tables, optimizing queries, using caching, and archiving old data.




14. HTTP Status Codes:

    - 200 OK: Success.

    - 201 Created: Resource successfully created.

    - 400 Bad Request: The request could not be understood or was missing required parameters.

    - 404 Not Found: Resource not found.

    - 500 Internal Server Error: General server error.




15. REST API vs. SOAP API:

    - REST API: Uses HTTP methods (GET, POST, PUT, DELETE) and is typically lightweight.

    - SOAP API: A protocol that uses XML and typically involves more overhead.




16. Laravel Passport and Generate Key:

    - Laravel Passport: Provides OAuth2 authentication for APIs.

    - To generate a key: Run `php artisan passport:install`.




17. JavaScript Closures & Promises:

    - Closures: Functions that have access to variables from their outer scope even after the outer function has returned.

    - Promises: Objects representing the eventual completion (or failure) of an asynchronous operation and its resulting value.




18. Handling Exceptions in Laravel:

    - Use the `try-catch` block to handle exceptions. Laravel also provides a global exception handler located in `app/Exceptions/Handler.php`.




19. var, let, and const Difference:

    - var: Function-scoped, can be re-declared and updated.

    - let: Block-scoped, can be updated but not re-declared.

    - const: Block-scoped, cannot be updated or re-declared.




20. Implementing REST API Bearer Key:

    - Implement a middleware that checks for the presence of a bearer token in the request headers, then validates the token before processing the request.




21. All Joins in SQL:

    - INNER JOIN: Returns records that have matching values in both tables.

    - LEFT JOIN: Returns all records from the left table, and the matched records from the right table.

    - RIGHT JOIN: Returns all records from the right table, and the matched records from the left table.

    - FULL JOIN: Returns all records when there is a match in either left or right table.




22. Foreign Key in SQL:

    - A foreign key is a column that creates a relationship between two tables, ensuring that the values in this column correspond to valid values in another table.




23. All Config Files and Their Usage:

    - In Laravel, config files are stored in the `config` directory and define settings for different parts of the application, such as database connections, caching, mail, and more.




24. Events in Laravel:

    - Events provide a way to execute code in response to certain actions, such as user registration. You can create and listen for events to decouple parts of your application.




25. What is Composer?:

    - Composer is a dependency manager for PHP that allows you to install, update, and manage packages and libraries required by your PHP project.




26. What is Artisan?:

    - Artisan is the command-line interface for Laravel, offering various commands for tasks like migrations, routing, and creating controllers.




27. What is a Model?:

    - A model in Laravel represents a database table and provides methods to interact with the data, such as retrieving, inserting, updating, and deleting records.




28. Fetch vs. Pull in Git:

    - Fetch: Downloads commits, files, and refs from a remote repository into your local repository, without merging.

    - Pull: Fetches changes from a remote repository and immediately merges them into your local branch.




29. Types of Relationships in Laravel:

    - One to One

    - One to Many

    - Many to Many

    - Has Many Through

    - Polymorphic Relationships




30. MCQ Questions on SQL, DB Use, Alter Commands, PHP, Database Drop Command, Echo vs Print:

    - Practice questions could include SQL syntax, differences between echo and print, `ALTER TABLE` commands, database dropping syntax, etc.




31. ECMAScript ES7, ES8:

    - ES7 (2016): Introduced `Array.prototype.includes()` and the exponentiation operator (``).

    - ES8 (2017): Introduced `async/await`, `Object.values()`, `Object.entries()`, and string padding methods.


These explanations cover the key points for each topic. If you need more detailed explanations or examples, feel free to ask!


-------------------------------------------------------------------------

PHP provides a wide range of built-in functions for working with strings and arrays. Here's an overview of some commonly used string and array methods:


 String Methods


1. `strlen($string)`:

   - Returns the length of the string.

   - Example: `strlen("Hello")` returns `5`.


2. `strpos($haystack, $needle)`:

   - Finds the position of the first occurrence of a substring in a string.

   - Example: `strpos("Hello World", "World")` returns `6`.


3. `str_replace($search, $replace, $subject)`:

   - Replaces all occurrences of the search string with the replacement string.

   - Example: `str_replace("World", "PHP", "Hello World")` returns `"Hello PHP"`.


4. `substr($string, $start, $length)`:

   - Returns a portion of the string.

   - Example: `substr("Hello World", 0, 5)` returns `"Hello"`.


5. `strtolower($string)`:

   - Converts all characters in a string to lowercase.

   - Example: `strtolower("HELLO")` returns `"hello"`.


6. `strtoupper($string)`:

   - Converts all characters in a string to uppercase.

   - Example: `strtoupper("hello")` returns `"HELLO"`.


7. `trim($string, $character_mask)`:

   - Strips whitespace (or other characters) from the beginning and end of a string.

   - Example: `trim(" Hello ")` returns `"Hello"`.


8. `explode($delimiter, $string)`:

   - Splits a string by a specified delimiter into an array.

   - Example: `explode(" ", "Hello World")` returns `["Hello", "World"]`.


9. `implode($glue, $pieces)`:

   - Joins array elements into a single string with a specified glue.

   - Example: `implode(", ", ["Hello", "World"])` returns `"Hello, World"`.


10. `str_repeat($string, $multiplier)`:

    - Repeats a string a specified number of times.

    - Example: `str_repeat("Hello", 3)` returns `"HelloHelloHello"`.


11. `strstr($haystack, $needle)`:

    - Finds the first occurrence of a string within another string, returning the rest of the string.

    - Example: `strstr("Hello World", "World")` returns `"World"`.


12. `strcmp($str1, $str2)`:

    - Compares two strings lexicographically.

    - Example: `strcmp("Hello", "Hello")` returns `0` (same), `strcmp("Hello", "World")` returns a negative number (different).


 Array Methods


1. `count($array)`:

   - Returns the number of elements in an array.

   - Example: `count([1, 2, 3])` returns `3`.


2. `array_push($array, $value1, $value2, ...)`:

   - Adds one or more elements to the end of an array.

   - Example: `array_push($array, "value")` adds `"value"` to the end of `$array`.


3. `array_pop($array)`:

   - Removes the last element from an array and returns it.

   - Example: `array_pop([1, 2, 3])` returns `3`.


4. `array_shift($array)`:

   - Removes the first element from an array and returns it.

   - Example: `array_shift([1, 2, 3])` returns `1`.


5. `array_unshift($array, $value1, $value2, ...)`:

   - Adds one or more elements to the beginning of an array.

   - Example: `array_unshift($array, "value")` adds `"value"` to the start of `$array`.


6. `array_merge($array1, $array2)`:

   - Merges two or more arrays into one.

   - Example: `array_merge([1, 2], [3, 4])` returns `[1, 2, 3, 4]`.


7. `in_array($needle, $haystack)`:

   - Checks if a value exists in an array.

   - Example: `in_array("apple", ["apple", "banana"])` returns `true`.


8. `array_search($needle, $haystack)`:

   - Searches for a value in an array and returns its key.

   - Example: `array_search("apple", ["apple", "banana"])` returns `0`.


9. `array_key_exists($key, $array)`:

   - Checks if a specified key exists in an array.

   - Example: `array_key_exists("name", ["name" => "John", "age" => 30])` returns `true`.


10. `array_slice($array, $offset, $length)`:

    - Extracts a portion of an array.

    - Example: `array_slice([1, 2, 3, 4], 1, 2)` returns `[2, 3]`.


11. `array_map($callback, $array)`:

    - Applies a callback function to each element of an array and returns a new array.

    - Example: `array_map('strtoupper', ['a', 'b', 'c'])` returns `['A', 'B', 'C']`.


12. `array_filter($array, $callback)`:

    - Filters elements of an array using a callback function.

    - Example: `array_filter([1, 2, 3, 4], fn($x) => $x % 2 == 0)` returns `[2, 4]`.


13. `array_reduce($array, $callback, $initial)`:

    - Reduces an array to a single value using a callback function.

    - Example: `array_reduce([1, 2, 3], fn($carry, $item) => $carry + $item)` returns `6`.


14. `array_column($array, $column_key, $index_key = null)`:

    - Returns the values from a single column in the input array, optionally indexed by another column.

    - Example: `array_column($array, 'name')` where `$array` is a list of associative arrays.


15. `array_combine($keys, $values)`:

    - Creates an array by using one array for keys and another for its values.

    - Example: `array_combine(['a', 'b'], [1, 2])` returns `['a' => 1, 'b' => 2]`.


These methods provide powerful tools for handling strings and arrays in PHP, allowing you to manipulate data efficiently and effectively.




Comments

Popular posts from this blog

IMPORTANT INTERVIEW RELATED QUESTION

C++ Interview Questions

SUBLIME TEXT CHEAT SHEET