Skip to main content

Posts

Showing posts from June, 2023

How to delete multiple Git branches using pattern Matching

To delete multiple branches using Git with pattern matching, you can use a combination of Git commands and shell scripting. Here's an example of how you can achieve this:  1. Open a terminal or command prompt.  2. Navigate to your Git repository directory.  3. Use the following command to list all branches matching a specific pattern: git branch | grep "<pattern>" Replace <pattern> with the pattern you want to match. For example, if you want to delete all branches starting with "feature/" , you can use feature/* as the pattern.  This command lists all branches that match the specified pattern.  4. Review the branch names listed and make sure they are the ones you want to delete.  5. Once you're sure, you can use the following command to delete the branches: git branch | grep "<pattern>" | xargs git branch -D This command combines the previous `git branch` and `grep` commands to find the branches matching ...

List of latest and most asked PHP practical interviews questions & answers

Core PHP Practical Interview Questions In this blog post I am sharing a list of some most asked PHP interview questions & answers. These are very useful and helpful for the freshers and experienced developer too. I have taken these questions from different sources and listed here at one place. Ques. How to reverse a string without using any builtin function? Ans: <?php $str = 'My name is Diwakar Kumar'; $len = 0; while(isset($str[$len]) != '') $len++; for($i = $len ; $i >= 0 ; $i--) { echo @$str[$i]; } Ques: Write a function to check if a given string is a palindrome or not. Ans: 1st Method: <?php function isPalindrome($str) { $str = strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $str)); // Convert to lowercase and remove non-alphanumeric characters $reverse = strrev($str); // Reverse the string return $str === $reverse; // Compare original and reversed string } 2nd Method: <?php funct...

How to take user input from terminal(stdin) in Rust?

In Rust, you can use the std::io module from the standard library to read input from the user. Here's an example that demonstrates how to get input from the user: use std::io; fn main() { // Create a new instance of `std::io::stdin` for reading user input let mut input = String::new(); // Prompt the user for input println!("Enter your name:"); // Read input from the user io::stdin() .read_line(&mut input) .expect("Failed to read line"); // Trim any trailing whitespace or newlines from the input let name = input.trim(); // Display the user's input println!("Hello, {}!", name); } In this example, we create a mutable String variable named input to store the user's input. We then use the std::io::stdin() function to obtain a handle to the standard input stream. Next, we call the read_line() method on the input stream, passing a mutable reference to the input variable. The r...

Explain Buffer overflow in Rust with example.

Buffer overflow is a common type of vulnerability that occurs when a program writes data beyond the boundaries of a buffer, leading to memory corruption and potential security issues. However, Rust's memory safety guarantees and ownership system help prevent buffer overflows by detecting and preventing such errors at compile-time. Rust's string handling and array bounds checking provide built-in protection against buffer overflows. Here's an example of how Rust mitigates buffer overflow: fn main() { let mut buffer = [0u8; 4]; // Buffer of size 4 let data = [1u8, 2u8, 3u8, 4u8, 5u8]; // Data larger than buffer size // Uncommenting the line below would result in a compilation error. // buffer.copy_from_slice(&data); // Attempt to write data into buffer println!("Buffer: {:?}", buffer); }  In this example, we have a fixed-size buffer with a capacity of 4 bytes ([0u8; 4]) and a data array (data) with a length of 5 bytes. The intention i...

What is null pointer dereferences in Rust?

In Rust, null pointer dereferences, also known as null pointer errors or null reference errors, refer to situations where a program attempts to access or dereference a null or uninitialized pointer. However, Rust's ownership and borrowing system and its lack of null pointers make null pointer dereferences virtually non-existent.  Rust's approach to null safety revolves around the concept of ownership and borrowing, which eliminates the need for null pointers and effectively prevents null pointer dereferences at compile-time. Instead of allowing null values, Rust uses the `Option` type to represent the presence or absence of a value.  The `Option` type is an enum with two variants: `Some(value)` to represent the presence of a value, and `None` to represent the absence of a value. By using `Option` types, Rust enforces explicit handling of potentially missing values, ensuring that developers handle the absence case explicitly, rather than encountering unexpected null pointer der...

Explain type inference in Rust with examples.

Type inference in Rust allows the compiler to deduce the types of variables and expressions based on their usage, without the need for explicit type annotations. Here are a few examples that illustrate how type inference works in Rust:  1. Variable Initialization: let x = 42; let y = 3.14; let z = true; In this example, Rust uses type inference to determine the types of variables `x`, `y`, and `z` based on their initial values. The type of `x` is inferred as `i32` (a 32-bit signed integer) because the initial value is an integer literal. `y` is inferred as `f64` (a 64-bit floating-point number) because the initial value is a floating-point literal. `z` is inferred as `bool` because the initial value is a boolean literal.  2. Function Return Type Inference: fn add_numbers(x: i32, y: i32) -> i32 { x + y } In this example, the function `add_numbers` takes two parameters `x` and `y`, both of type `i32`, and performs their ad...

Is Rust a static typed language? Explanation with example.

Yes, Rust is a statically typed programming language. In Rust, every expression and variable has a type that is known at compile-time. This means that the type of every value is checked and verified by the Rust compiler before the program is executed. Here are some key points that explain Rust's static typing in detail: 1. Type Inference: Although Rust is statically typed, it employs type inference to infer the types of variables whenever possible. This means that in many cases, you don't need to explicitly annotate the types of variables, as the compiler can deduce them based on their usage. This allows for more concise code without sacrificing type safety. 2. Strong Type System: Rust has a strong type system, which means that the type of a value is strictly enforced. Operations and functions in Rust can only be performed on values of the appropriate types, and the compiler will flag any type mismatches or incompatible operations. 3. Type Annotations: While type inference ...

Why the computer's keys are not in the alphabetical order?

The arrangement of keys on a computer keyboard is not in alphabetical order primarily due to historical reasons and the evolution of typewriters and early computer keyboards. Let's take a journey through the evolution of keyboard layouts to understand why they are designed the way they are today. 1. QWERTY Layout : Qwerty Layout   The most widely used keyboard layout is the QWERTY layout, named after the first six letters in the top row of keys. It was designed in the 1870s for mechanical typewriters by Christopher Sholes to prevent mechanical jams. The layout was optimized for the mechanical linkages of the time to avoid key clashes by placing commonly used letters apart from each other. This arrangement helped reduce the likelihood of adjacent keys being struck in quick succession, minimizing the likelihood of jamming. 2. Sholes' Typewriter: Christopher Sholes designed the first practical typewriter with the QWERTY keyboard layout. It gained popularity and became the standar...

How to use terminal within the Sublime Text editor?

Sublime Text is primarily a text editor and does not have a built-in terminal like some other integrated development environments (IDEs) do. However, you can use the terminal from within Sublime Text by installing a package called Terminal and Terminus . To use the terminal in Sublime Text using Terminal package, follow these steps: Install Package Control (if you haven't already): Open Sublime Text. Press Ctrl + (backtick) to open the Sublime Text console. Copy and paste the installation code for Package Control from the official website: https://packagecontrol.io/installation Press Enter to execute the code. Wait for Package Control to install. Install the "Terminal" package: Press Ctrl + Shift + P (Windows/Linux) or Cmd + Shift + P (Mac) to open the command palette. Type "Package Control: Install Package" and select it from the command palette. In the package list, type "Terminal" and select the "Terminal" package to install it. Open t...