Q-1. Given a = 25 and b = 4, perform all arithmetic operations and display the results.
var a = 25;
var b = 4;
var c;
c = ("Sum: " + (a + b));
c = ("Difference: " + (a - b));
c = ("Product: " + (a * b));
c = ("Quotient: " + (a / b));
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 = 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'); // 55 (String concatenation)
document.write(5 - '2'); // 3 (String is converted to number)
Q-5. What is the result of: let x = 10 % 3?
Answer: let x = 10 % 3; // 1
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.
let x = 50;
x += 10; // 60
x -= 5; // 55
x *= 2; // 110
x /= 5; // 22
document.write(x);
Q-8. What will be the value of a after this operation: let a = 20; a %= 7;?
Answer: 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;
let x = 5;
x = x + 10; // 15
x += 10; // Also 15, shorthand version
Q-11. What will be the output of this code?
let a = 5;
document.write(a++); // 5
document.write(a); // 6
Q-12. Difference between prefix and postfix increment operators.
let x = 5;
document.write(++x); // 6
document.write(x++); // 6 (then x becomes 7)
Q-13. What will be the output of this code?
let x = 3;
let y = ++x + x++;
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: 9
Q-16. What's the difference between 5 == '5' and 5 === '5'?
Answer: 5 == '5' is true, 5 === '5' is false
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 >= 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?
Answer: Yes
Q-20. What will be the result of:
let a = 20, b = 25;
document.write(a != b); // true
Q-21. Check if person can vote (age >= 18 and nationality = 'Indian').
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
false || true => true
!false => true
Q-23. Write a login check.
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 for age check.
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. Ternary operator for pass/fail.
let marks = 35;
document.write(marks >= 40 ? "Pass" : "Fail");
Q-28. Even or Odd check using ternary.
let x = 5;
let res = (x % 2 == 0) ? 'Even' : 'Odd';
document.write(res); // Odd
Q-29. Grade system using nested ternary.
let marks = 90;
let grade = (marks >= 95) ? 'A' :
(marks >= 65) ? 'B' :
(marks >= 70) ? 'C' : 'Fail';
document.write(grade); // B
Q-30. Output of:
let name = "Rahul";
let msg = "Hello " + name;
document.write(msg); // Hello Rahul
Q-31. What happens when a number and string are combined using +?
Answer: Concatenation. 10 + '5' => "105"
Q-32. Predict the output:
let a = 10;
let b = 20;
let msg = "Sum is " + (a + b);
console.log(msg); // Sum is 30
Q-33. Concatenate first and last name.
let first = "Ravi";
let last = "Kumar";
document.write("Full Name: " + first + " " + last);
Q-34. Take two string inputs and show greeting.
let fname = prompt("Enter first name:");
let lname = prompt("Enter last name:");
document.write("Welcome " + fname + " " + lname + "!");