KEMBAR78
QB Operators | PDF | Computer Science | Computer Engineering
0% found this document useful (0 votes)
6 views10 pages

QB Operators

The document contains a series of programming questions and answers related to basic arithmetic operations, variable manipulation, and control structures in JavaScript. It includes examples of using operators, conditional statements, loops, and user input to perform calculations and display results. The document also explains concepts like type coercion, ternary operators, and the difference between equality checks.

Uploaded by

tanaynagare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

QB Operators

The document contains a series of programming questions and answers related to basic arithmetic operations, variable manipulation, and control structures in JavaScript. It includes examples of using operators, conditional statements, loops, and user input to perform calculations and display results. The document also explains concepts like type coercion, ternary operators, and the difference between equality checks.

Uploaded by

tanaynagare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Q-1.

Given a = 25 and b = 4, perform all arithmetic operations and display the


results.
var a = 25,
var b = 4 ;
var c;
var c=("Sum: " + (a + b));
var c=("Difference: " + (a - b));
var c=("Product: " + (a * b));
var c=("Quotient: " + (a / b));
var c=("Remainder: " + (a % b));

Q-2. Write a program that takes two numbers from the user and prints their
sum, difference, product, quotient, and remainder.
no1 = Number(prompt("Enter first number"));
no2 = Number(prompt("Enter second number"));
document write ("Sum: " + (no1 + no2));
document write ("Difference: " + (no1 - no2));
document write ("Product: " + (no1 * no2));
document write ("Quotient: " + (no1 / no2));
document write ("Remainder: " + (no1 % no2));

Q-3. What is the output of this expression: (a + b) * c when a = 2, b = 3, c = 4?


Answer:
(2 + 3) * 4 = 5 * 4 = 20
document write ((a+b)*c);
Q-4. What will be the output of console.log(5 + '5') and console.log(5 - '2')?
Explain why.
Answer:
document write (5 + '5');
document write (5 - '2');

Q-5. What is the result of: let x = 10 % 3;?


Answer:
let x = 10 % 3; // 1
document write ((a+b)*c);

Q-6. What is the output of the following code?


let x = 10;
x += 5; // 15
x *= 2; // 30
document write (x);
Output: 30

Q-7. *Write a program that starts with x = 50 and uses +=, -=, =, /= operators
step by step to modify x.
let x = 50;
x += 10; // 60
x -= 5; // 55
x *= 2; // 110
x /= 5; // 22
document write (x);
Output: 22

Q-8. What will be the value of a after this operation: let a = 20; a %= 7;?
Answer:
let a = 20;
a %= 7; // 6

Q-9. Create a mini calculator that accepts values and modifies them using
assignment operators.
let value = Number(prompt("Enter initial value:"));
value += 5;
value -= 2;
value *= 3;
value /= 2;
document write ("Final value: " + value);
Q-10. Explain the difference between x = x + 10; and x += 10; with examples.
Answer:
let x = 5;
x = x + 10; // x becomes 15

Q-11. What will be the output of this code?


let a = 5;
document write (a++); // 5
document write (a); // 6

Q-12. What is the difference between prefix and postfix increment/decrement


operators? Show with examples.
Answer:
let x = 5;
document write (++x);
document write (x++);

Q-13. What will be the output of this code?


let x = 3;
let y = ++x + x++;
// y = 4 + 4 = 8
document write (y); // 8
Q-14. Write a loop that prints numbers from 1 to 10 using ++.
for (let i = 1; i <= 10; i++) {
document write (i);
}

Q-15. If let count = 10; count--; count--; count++;, what is the final value of
count?
Answer:
Final value = 10 - 1 - 1 + 1 = 9

Q-16. What's the difference between 5 == '5' and 5 === '5'?


Answer:
5 == '5' // true (checks value only)
5 === '5' // false (checks value and type)

Q-17. Predict the output:


let a = 10, b = '10';
document write (a == b); // true
document write (a === b); // false
Q-18. Write a program that checks if the user’s age is greater than or equal to
18.
let age = Number(prompt("Enter your age:"));
if (age >= 18) {
document write ("You are eligible.");
} else {
document write ("You are not eligible.");
}

Q-19. Is 10 <= 10 and 10 >= 10 true? Why?


Answer:
Yes, both are true because 10 is equal to 10, so both comparisons hold.
Q-20. What will be the result of:
let a = 20, b = 25;
document write (a != b); // true

Q-21. Check whether a person is eligible to vote (age ≥ 18 and nationality =


'Indian') using logical operators.
let age = 20;
let nationality = "Indian";
if (age >= 18 && nationality === "Indian") {
document write ("Eligible to vote");
} else {
document write (Not eligible");
}
Q-22. What are the results of: true && false, false || true, and !false?
Answer:
true && false
false || true
!false

Q-23. Write a login check that ensures both username and password are
entered.
let user = prompt("Enter username:");
let pass = prompt("Enter password:");
if (user && pass) {
document write ("Login successful");
} else {
document write ("Username and password required");
}

Q-24. What will this return?


let x = 5;
document write (x > 3 && x < 10); // true
document write (!true || false && true); // false

Q-25. Write a ternary statement that returns "Eligible" if age ≥ 18, otherwise
"Not Eligible".
let age = 17;
let msg = (age >= 18) ? "Eligible" : "Not Eligible";
document write (msg);

Q-26. What is the output?


let a = 10, b = 20;
let result = (a > b) ? a : b;
document write (result); // 20

Q-27. Using a ternary operator, print "Pass" if marks ≥ 40, else "Fail".
let marks = 35;
document write (marks >= 40 ? "Pass" : "Fail");

Q-28. What does this return?


let x = 5;
let res = (x % 2 == 0) ? 'Even' : 'Odd';
document write (res); // "odd"

Q-29. Create a grade system using nested ternary:


let marks = 90;
let grade = (marks >= 95) ? 'A' :
(marks >= 65) ? 'B' :
(marks >= 70) ? 'C' : 'Fail';
(grade); // B
Q-30. What is the output of:
let name = "Rahul";
let msg = "Hello " + name;
document write (msg);

Q-31. What happens when a number and string are combined using +?
Answer:
Concatenation happens, number is converted to string.
10 + '5' → "105"

Q-32. Predict the output:


let a = 10;
let b = 20;
let msg = "Sum is " + (a + b);
console.log(msg);

Q-33. Combine first and last name using concatenation and print full name.
let first = "Ravi";
let last = "Kumar";
document write ("Full Name: " + first + " " + last);

Q-34. Take two string inputs and show a greeting message using +.
let fname = prompt("Enter first name:");
let lname = prompt("Enter last name:");
document write ("Welcome " + fname + " " + lname + "!");

You might also like