Ask Programiz - Human Support for Coding Beginners

Explore questions from fellow beginners answered by our human experts. Have some doubts about coding fundamentals? Enroll in our course and get personalized help right within your lessons.

  • All
  • Python
  • C
  • Java
  • CPP
  • SQL
  • JS
  • HTML
  • CSS
Abhay Jajodia
Expert
last week
Abhay Jajodia answered

Good question! It’s essential to allocate the correct amount of memory for the data types we want to store.

When you use malloc(n * sizeof(double)), you're telling the program to allocate memory for n number of double values. Each double typically takes 8 bytes of memory. Therefore, by calculating n * sizeof(double), you're ensuring you get enough space for all the doubles you intend to store. If we only allocated ptr with an address without specifying the correct size, there might not be enough memory for all your doubles, leading to errors or program crashes.

So, we need to explicitly allocate n * sizeof(double) to ensure we have enough memory for all n double values you want to work with. Hope this helps!

C
This question was asked as part of the Learn C Programming course.
Abhay Jajodia
Expert
last week
Abhay Jajodia answered

Hello Prinnie! The issue is with your SQL statement is that you can't use multiple ORDER BY clauses in the same query.

Instead, you should separate the columns by commas within a single ORDER BY clause. For example, if you want to sort the results first by name, then by country, and finally by purchase_amount, you can do it like this:

SELECT *
FROM Customers
ORDER BY name, country, purchase_amount;

This way, all the sorting is done in one go, and you will get the results in the order you expect. Let me know if you have more questions!

Happy Coding!!!
SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
last week
Abhay Jajodia answered

Hello Ramyasri.

An exception is not raised automatically for every error.
A catch block runs only if something is thrown.

Case 1: When you don’t need throw

Some C++ operations already know how to handle failure.
Memory allocation with new is one of them.

If new fails:

  • C++ automatically throws an exception

  • Control jumps directly to catch

  • You don’t need to write throw yourself

So it feels like “magic”, but it’s actually built-in behavior.


Case 2: When you do need throw

In normal program logic (conditions, calculations, validations) and the current task in the lesson is calculation:

  • C++ does not assume anything is an error

  • Even if something is “wrong” by your logic, C++ keeps running

  • No exception exists unless you explicitly create one

So unless you write throw, there is:

  • No exception

  • Nothing for catch to catch

  • No error message

I hope I was able to explain it clearly. If you still have any confusion, please feel free to message me again and I’ll be happy to explain it in more detail.

Thank you for your question. keep up the great work in your learning journey! I look forward to hearing more questions from you.

C++
This question was asked as part of the Learn C++ Intermediate course.
N
Expert
last week
Nisha Sharma answered

Yes. A catch block only runs if something is thrown inside the try block.

In your example, throw 0; is what triggers the exception. When denominator == 0, the program throws 0, skips the remaining lines in the try block, and jumps straight to:

catch (int num) { ... }

If the denominator isn’t 0, nothing gets thrown, so the catch block is skipped and the program continues normally.

Feel free to reach out if you have any more queries.

C++
This question was asked as part of the Learn C++ Intermediate course.
Udayan Shakya
Expert
last week
Udayan Shakya answered

Hello Rocky! I see you're taking your Java lessons earnestly, and it's really encouraging to see learners like you!

And your hunch about type declaration is right on the mark:

Every time you declare a new variable, you need to specify its type. You don't need to specify type for the variable after you've declared it.

Let's clarify this with a modified version of the program you've given:

class Main {
    public static void main(String[] args) {
     
        // Create and print the distance variable
        int distance = 134;
        System.out.println("Original distance = " + distance);
     
        // Create another variable distance2
        int distance2 = 564;
        System.out.println("Original distance2 = " + distance2);
     
        // Assign another value to distance
        // No need to declare type again
        distance = 760;
        System.out.println("\nModified distance = " + distance);
     
        // Assign another value to distance2
        // No need to declare type again
        distance2 = 666;
        System.out.println("Modified distance2 = " + distance2);
     }
}

Output

Original distance = 134
Original distance2 = 564

Modified distance = 760
Modified distance2 = 666

Notice the following blocks of code above:

distance = 760;
System.out.println("\nModified distance = " + distance);
     
distance2 = 666;
System.out.println("Modified distance2 = " + distance2);

You can clearly see that you don't need to redeclare the types of distance and distance2 because they've already been declared when you created the variables:

// Declaration of 'distance' variable
int distance = 134;


// Declaration of 'distance2' variable
int distance2 = 564;

This is true not just for Java, but for other programming languages like C, C++, TypeScript, etc. as well.

Hope that clears things up! Contact us again if you have further questions!

Java
This question was asked as part of the Learn Java Basics course.
Udayan Shakya
Expert
last week
Udayan Shakya answered

Hello again, Stephen! I'm not exactly clear about what you're asking, but I'm guessing you're asking one of two questions (or both) I've answered below.

Q1) What's the mathematical operation we're performing?

The calculate_sum() function calculates the sum from 1 to n, where n is the argument passed to it.

Here, n = 3, so calculate_sum() should return:

calculate_sum(3) = 1 + 2 + 3 = 6

==============================================

Q2) How does this recursive function work?

Recursion is a confusing topic that even many of us still struggle with from time to time.

What's important to remember about recursion is this:

The recursive function doesn't start returning values until the base case is reached.

In our case, the base case is n == 1.

So, calculate_sum(3) will recursively call itself until calculate_sum(1) is called.

Once we've reached this base case, calculate_sum(1) will return a value to calculate_sum(2), which in turn returns a value to calculate_sum(3).

Here's the sequence of the recursive calls:

calculate_sum(3) --->calculate_sum(2) --->calculate_sum(1)

Here's the sequence of value returns:

calculate_sum(1) --->calculate_sum(2) --->calculate_sum(3)

I'll try to explain it to the best of my abilities. Please try to read this explanation alongside the image we've provided in our Recursion course:

Part 1: Successive Function Calls

  1. First, result = calculate_sum(3) is executed, which calls the calculate_sum() function with 3 as argument. Let's simply call this function call as calculate_sum(3)

  2. Notice the statement return n + calculate_sum(n - 1);. Inside calculate_sum(3), this statement becomes return 3 + calculate_sum(2).

  3. Here, the return statement isn't fully executed. Instead, it calls calculate_sum(2) first.

  4. In other words, a separate instance of calculate_sum() is called (with 2 as an argument). Thus, calculate_sum(2) is separate from calculate_sum(3). Please remember that no value has been returned so far.

  5. Similarly, calculate_sum(2) executes return 2 + calculate_sum(1). This means a separate function calculate_sum(1) is called without returning any value.

Part 2: Successive Value Returns

  1. We left off at when calculate_sum(1) is called. Now, let's pick off right where we left off.

  2. calculate_sum(1) directly returns the value 1 without calling any further functions. That's because calculate_sum(1) is the base case where the if (n == 1) statement is executed.

  3. This return value of calculate_sum(1) then goes back to calculate_sum(2).

  4. Remember that calculate_sum(2) executes return 2 + calculate_sum(1). Since calculate_sum(1) returned 1, this return statement becomes return 2 + 1, i.e., return 3.

  5. In other words, calculate_sum(2) returns the value 3 to calculate_sum(3).

  6. Then, calculate_sum(3) executes return 3 + calculate_sum(2), which is return 3 + 3, i.e., return 6.

  7. Thus, calculate_sum(3) returns the value 6 to result.

And that's how we get 6 as the final result.

Hope that helps! Contact us again if you have further questions.

C
This question was asked as part of the Learn Recursion With C course.
Abhay Jajodia
Expert
2 weeks ago
Abhay Jajodia answered

Hi Rishabh! That's a great question and a common point of confusion for many learners just starting with SQL. Let's dive into it:

Why Use VARCHAR for Phone Numbers?

Even though phone numbers consist of digits, we treat them differently from regular numbers like age or ID. Here's why:

  • Fixed Format: Phone numbers often have leading zeros or country codes, and using an int would strip those away (e.g., +04123456789).

  • Non-Numeric Characteristics: Since phone numbers aren't used for calculations (like operations or comparisons), they are better stored as strings.

Give it a try and let me know if you run into any issues or have further questions. Happy querying! 🙂

SQL
This question was asked as part of the Learn SQL Basics course.
Alice Yuan
PRO
2 weeks ago
Alicecountry asked
Abhay Jajodia
Expert
2 weeks ago
Abhay Jajodia answered

Hi Alice 😊

A command line argument is a value you pass to a program when you run it from the terminal. These values tell the program what data it should work with or what action it should perform.

For example, if you have a program that adds numbers, you can run it like this:

./add 10 20

Here, 10 and 20 are the command line arguments. The program reads these values and uses them to calculate the result.

I hope this explains the concept clearly 😊 Please let me know if you need any further help.

C
This question was asked as part of the Learn C Programming course.
Anuj Shrestha
Expert
2 weeks ago
Anuj Shrestha answered

In Python, semicolons are optional and are generally not used. You can add one at the end of a line, but it isn’t required.

Feel free to try the code below in the editor on the right (or switch to the Code tab if you’re on mobile):

print("Hello, World!");

You'll see this code works just fine.

And I see you’ve started your Python journey. Nice... If you have more questions along the way, just let me know.

Python
This question was asked as part of the Learn Python Basics course.
Anuj Shrestha
Expert
2 weeks ago
Anuj Shrestha answered

Hi! Good observation but the summary is actually correct, and the confusion usually comes from how the word immutable is used.

In Python, a set itself is mutable. This means you can add or remove items from a set after it’s created, which is why the table correctly marks Set → Mutable: Yes.

immutable are the elements inside a set. Every item stored in a set must be immutable (for example: numbers, strings, or tuples). This is why you cannot put lists or other sets inside a set.

So to summarize:

  • Set (container): Mutable ✅

  • Elements inside a set: Must be immutable ✅

If a lesson mentioned that “sets are immutable,” it was likely referring to the elements of a set, not the set itself.

Hope this clears your confusion. Feel free to ask again. I will take a look at the previous lesson too to make sure we are clarifying it properly. Thanks for asking.

Python
This question was asked as part of the Learn Python Basics course.