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 year
Abhay Jajodia answered

Hi Soumyadeep,

Yes, it’s easier than it seems! Let’s take a simple example using the OR operator (||), which is one kind of logical gate.

The OR operator checks two conditions and gives true if at least one of them is true.

Here’s how it works in code:

if ((age >= 18) || (gpa > 3.5)) {
    printf("You meet the criteria.\n");
}

This line means:
“If the person is 18 or older, or their GPA is above 3.5, then print the message.”

Examples:

  • If age is 20 and GPA is 3.0 → it prints (because age ≥ 18)

  • If age is 16 and GPA is 3.8 → it prints (because GPA > 3.5)

  • If age is 16 and GPA is 3.0 → it doesn’t print (because both are false)

So in simple terms:
OR means only one condition needs to be true for the code to run.

Let me know if you want help understanding other gates like AND or NOT — I’m here to help.

C
This question was asked as part of the Learn C Programming course.
Gowtham
PRO
last year
Gowthamcountry asked
Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Gowtham,

Good question! Let’s break it down simply.

A bit is the smallest unit of data in a computer. It's either a 0 or a 1.

But in practice, we usually deal with bytes, and 1 byte = 8 bits.

So when we talk about how many bits something uses, we’re really asking how much space it takes in memory. For example:

  • char = 1 byte = 8 bits

  • int = 4 bytes = 32 bits

  • double = 8 bytes = 64 bits

You can check this in C++ using the sizeof operator:

#include 
using namespace std;

int main() {
    cout << "char: " << sizeof(char) << " byte" << endl;
    cout << "int: " << sizeof(int) << " bytes" << endl;
    cout << "double: " << sizeof(double) << " bytes" << endl;
    return 0;
}

The output tells you how many bytes each type uses. Just multiply that by 8 to get the number of bits.

So bits are calculated based on the data type and how much space it uses in memory.

If you’d like help with binary values or how bits are used in operations, I’m here to help.

C++
This question was asked as part of the Learn C++ Basics course.
Kelish Rai
Expert
last year
Kelish Rai answered

Hi Jayadithya,

Great question — this trips up a lot of learners.

The sizeof() operator does return the correct value — it gives you the size of a type or variable in bytes. But here’s the key part:
printf() doesn’t know what type you're printing unless you tell it using a format specifier.

So even if sizeof(int) returns a valid number like 4, you still need to use %d or %lu (depending on your system and compiler) to tell printf how to interpret and display that value.

Now, about using different format specifiers — yes, that’s sometimes intentional. For example:

char ch = 'A';
printf("%d", ch);  // Prints 65 instead of 'A'

Here, %d is used to show the numeric (ASCII) value of the character, not the character itself.

So to sum up:

  • sizeof() works fine — the issue is with how you choose to print the result.

  • Sometimes, using a "wrong" format specifier is done on purpose to view data differently.

If you have more questions, I am here to help.

C
This question was asked as part of the Learn C Programming course.
Kelish Rai
Expert
last year
Kelish Rai answered

Hi quiet,

You're absolutely right .

In Java, Math.random() returns a value from 0.0 (inclusive) to 1.0 (exclusive). So:

Math.random() * 10    // gives a value from 0.0 to less than 10.0  
Math.random() * 10 + 1  // gives a value from 1.0 to less than 11.0

So the correct range for Math.random() * 10 + 1 is 1.0 (inclusive) to less than 11.0 (exclusive).

If you have more questions, I am here to help.

Java
This question was asked as part of the Learn Java Basics course.
Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Shawn,

The main difference is in how they’re defined:

  • List → uses square brackets []

    my_list = [1, 2, 3]
    
  • Tuple → uses parentheses ()

    my_tuple = (1, 2, 3)
    

If you're creating a tuple with just one item, you need a comma — otherwise, Python won’t treat it as a tuple:

single_item = (1)     # This is just the number 1  
single_item_tuple = (1,)  # This is a tuple with one item

Lists don’t need a comma for single items, and you can freely change (mutate) them — unlike tuples, which are immutable.

If you have more questions, I am here to help.

Python
This question was asked as part of the Getting started with Python course.
Amogh Jasthi
PRO
last year
Amoghcountry asked
Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Amogh,

In Python, the not keyword is a logical operator that flips the value of a condition:

  • not True becomes False

  • not False becomes True

So when you use if not, you're checking if a condition is not true.

Here’s a simple example:

is_raining = True

if not is_raining:
    print("You don't need to bring an umbrella")
else:
    print("Please bring an umbrella")

In this case, is_raining is True, so not is_raining becomes False, and the code inside the else block runs.

In short, if not is used when you want to run code only when a condition is false.

If you have more questions, I am here to help.

Python
This question was asked as part of the Getting started with Python course.
Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Timi,

Good question. It depends on how the function is defined.

Here’s your example:

def greet(message):
    print(message)

greet('Hi', 'Hello')

In this case, the function greet is defined to accept only one argumentmessage. But when you call it with two arguments ('Hi', 'Hello'), Python throws an error:

TypeError: greet() takes 1 positional argument but 2 were given

So actually, it doesn’t return one output — it raises an error because you passed more arguments than expected.

If you want the function to handle multiple messages, you can use *args, like this:

def greet(*messages):
    for message in messages:
        print(message)

greet('Hi', 'Hello')

Output:

Hi  
Hello

Using *messages lets the function accept any number of arguments and print each one.

If you have more questions, I am here to help.

Python
This question was asked as part of the Go beyond Python fundamentals course.
Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Ian,

Good question. You don’t need to declare the first number as 1. Instead, you can use a loop that runs 5 times and asks the user to enter a number each time. You just keep adding each input to a running total.

Here’s how you can do it in C++:

#include 
using namespace std;

int main() {
    int sum = 0;    // to store the total
    int number;     // to hold each number entered by the user

    for (int i = 1; i <= 5; i++) {
        cout << "Enter number " << i << ": ";
        cin >> number;
        sum += number;  // add the number to the total
    }

    cout << "Total sum is: " << sum << endl;
    return 0;
}

How it works:

  • You start with sum = 0 — the total will build up as users enter numbers.

  • A for loop runs 5 times (from 1 to 5).

  • In each iteration, the user enters a number, and it’s added to sum.

So, no need to start by declaring a number as 1. The loop and input take care of that.

If you have more questions, I am here to help.

C++
This question was asked as part of the Learn C++ Basics course.
Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Isabella,

You’re absolutely right — range-based for loops are a great way to simplify your code. But in your case, the reason it doesn’t work likely has to do with how arrays behave when passed to functions in C++.

Here's the issue:

When you pass an array like this:

void find_average(double elements[5]) { ... }

Inside the function, elements is actually treated as a pointer (double*), not a real array. The compiler loses the size information, which the range-based for loop depends on to work.

So this:

for (double number : elements)

won’t work because C++ doesn’t know how many elements to loop over.


✅ How to fix it

Option 1: Use a loop with an explicit size

void find_average(double* elements, int size) {
    double sum = 0.0;
    for (int i = 0; i < size; ++i) {
        sum += elements[i];
    }
}

Option 2: Use std::array or std::vector
These keep size information, so range-based loops work fine:

#include 

void find_average(const std::array& elements) {
    double sum = 0.0;
    for (double number : elements) {
        sum += number;
    }
}

So yes — range-based for loops do the same thing, but they only work when the size of the container is known. With plain arrays in functions, that size gets lost.

If you have more questions, I am here to help.

C++
This question was asked as part of the Learn C++ Basics course.
Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Kamohelo,

That error usually means you're trying to update something that doesn't exist in the DOM at the moment you're accessing it. In this case, you're trying to set the textContent of an element, but the variable holding that element is actually undefined.

This often happens when you're using something like element.children[2], but there are fewer than 3 children, so JavaScript can't find the item at index 2. Then, when you try to set textContent, it throws the error because you’re basically saying undefined.textContent = ..., which isn't valid.

Let’s say your code looks like this:

const priceList = document.querySelector("#price-list");
const update = document.querySelector("#update");

update.addEventListener("click", () => {
    const thirdItem = priceList.children[2];
    const thirdItemPrice = thirdItem.children[0];
    thirdItemPrice.textContent = "$4.00";
});

If thirdItem doesn’t exist, or if it doesn’t have a child at index 0, you’ll get that exact error.

To avoid it, you can add checks:

if (thirdItem && thirdItem.children.length > 0) {
    const thirdItemPrice = thirdItem.children[0];
    if (thirdItemPrice) {
        thirdItemPrice.textContent = "$4.00";
    } else {
        console.error("The child element you're trying to access doesn't exist.");
    }
} else {
    console.error("The third list item or its children do not exist.");
}

Also, using console.log() is super helpful — it lets you see what each variable actually holds before using it. That way, you can catch undefined values early and avoid these errors.

If you have more questions, I am here to help.

This question was asked as part of the JavaScript in Browser course.