0% found this document useful (0 votes)
20 views4 pages

Coding Set 2 Answers

The document contains 20 multiple choice questions and answers about Python strings and lists. It also contains 6 coding problems and solutions in both Python and Java.

Uploaded by

Clash Clan
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)
20 views4 pages

Coding Set 2 Answers

The document contains 20 multiple choice questions and answers about Python strings and lists. It also contains 6 coding problems and solutions in both Python and Java.

Uploaded by

Clash Clan
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/ 4

SET 2 PYTHON MCQ ANSWERS: 4.

a) "HelloWorld"

1. a) "HelloWorld" 5. c) `str.length`

2. b) Immutable 6. b) `toUpperCase()`

3. b) `len()` 7. b) No

4. b) `my_string.startswith("Hello")` 8. a) null

5. a) "123" 9. d) `substring()`

10. a) `str.contains(substring)`
6. d) Using parentheses: (...)
11. c) 'v'
7. b) `upper()`
12. a) `startsWith()`
8. a) Python uses zero-based indexing.
13. a) `String.valueOf(intValue)`
9. a) 5
14. a) `replace()`
10. a) \n
15. b) Using the `+` operator
11. a) "HelloHelloHello"
16. b) "JAVA"
12. a) `strip()`
17. c) `str.endsWith(suffix)`
13. a) `find()`
18. b) `strip()`
14. b) Using the `concat()` method
19. c) "av"
15. a) Splits a string into a list of substrings
20. c) `indexOf()`
based on a delimiter

16. b) "Hello, World!"


CODING ANSWERS:
17. c) "tho"
1. Addition of two numbers (read input from
18. a) `replace()`
user)
19. a) `isalpha()` num1 = float(input("Enter first number: "))
20. c) `endswith()` num2 = float(input("Enter second number: "))
SET 2 JAVA MCQ ANSWERS # Adding the numbers

1. a) String sum = num1 + num2

2. c) `String` is a final class, while # Displaying the result


`StringBuilder` is not.
print(f"The sum of {num1} and {num2} is
3. a) `equals()` {sum}")
2. Program to determine whether a given year sqrt = int(num**0.5)
is a leap year or not:
if sqrt*sqrt == num:
year = int(input("Enter a year: "))
print(f"{num} is a perfect square")
if (year % 4 == 0 and year % 100 != 0) or (year %
400 == 0): else:
print(f"{year} is a leap year") print(f"{num} is not a perfect square")
else:
6. Program to swap two numbers:
print(f"{year} is not a leap year")
# Program to swap two numbers
3. Program to check whether a given number is
positive or negative: num1 = int(input("Enter the first number: "))

num = float(input("Enter a number: ")) num2 = int(input("Enter the second number: "))

if num > 0:

print(f"{num} is positive") # Swapping the numbers


elif num < 0: temp = num1
print(f"{num} is negative")
num1 = num2
else:
num2 = temp
print("The number is zero")
# Displaying the swapped values
4. Program to find the square root of a number
print(f"After swapping, num1 is {num1} and
import math num2 is {num2}")

num = float(input("Enter a number: ")) JAVA CODING ANSWERS


if num >= 0: 1. Addition of two numbers (reading input
from the user):
sqrt = math.sqrt(num)
import java.util.Scanner;
print(f"The square root of {num} is {sqrt}")
public class AddTwoNumbers {
else:
public static void main(String[] args) {
print("Negative numbers do not have real
square roots") Scanner scanner = new Scanner(System.in);
5. Program to determine if a given number is a System.out.print("Enter first number: ");
perfect square:
double num1 = scanner.nextDouble();
num = int(input("Enter a number: "))
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble(); public static void main(String[] args) {

double sum = num1 + num2; Scanner scanner = new Scanner(System.in);

System.out.println("The sum of " + num1 + System.out.print("Enter a number: ");


" and " + num2 + " is " + sum);
double num = scanner.nextDouble();
}
if (num > 0) {
}
System.out.println(num + " is positive");
2. Program to determine whether a given year
is a leap year or not: } else if (num < 0) {

import java.util.Scanner; System.out.println(num + " is negative");

public class LeapYearChecker { } else {

public static void main(String[] args) { System.out.println("The number is


zero");
Scanner scanner = new Scanner(System.in);
}
System.out.print("Enter a year: ");
}
int year = scanner.nextInt();
}
if ((year % 4 == 0 && year % 100 != 0) ||
(year % 400 == 0)) { 4. Program to find the square root of a
number:
System.out.println(year + " is a leap
year"); import java.util.Scanner;

} else { public class SquareRootFinder {

System.out.println(year + " is not a leap public static void main(String[] args) {


year"); Scanner scanner = new Scanner(System.in);
} System.out.print("Enter a number: ");
} double num = scanner.nextDouble();
}

3. Program to check whether a given number is if (num >= 0) {


positive or negative:
double sqrt = Math.sqrt(num);
import java.util.Scanner;
System.out.println("The square root of "
public class PositiveNegativeChecker { + num + " is " + sqrt);
} else { public static void main(String[] args) {

System.out.println("Negative numbers Scanner scanner = new Scanner(System.in);


do not have real square roots");
System.out.print("Enter first number: ");
}
int num1 = scanner.nextInt();
}
System.out.print("Enter second number: ");
}
int num2 = scanner.nextInt();
5. Program to determine if a given number is a
perfect square: System.out.print("Enter third number: ");

import java.util.Scanner; int num3 = scanner.nextInt();

public class PerfectSquareChecker { int sum = num1 + num2 + num3;

public static void main(String[] args) { System.out.println("The sum of " + num1 +


", " + num2 + " and " + num3 + " is " + sum);
Scanner scanner = new Scanner(System.in);
}
System.out.print("Enter a number: ");
}
int num = scanner.nextInt();

int sqrt = (int) Math.sqrt(num);

if (sqrt * sqrt == num) {

System.out.println(num + " is a perfect


square");

} else {

System.out.println(num + " is not a


perfect square");

6. Program to find the addition of three


numbers (reading 3 integers from the user):

import java.util.Scanner;

public class AddThreeNumbers {

You might also like