What is wrong with the following code? Explain verbally.
s1 = "Canada"
s1[2] = ‘T’
What will the following code display? Explain verbally.
s1 = "Hawaii is a paradise"
print(s1[2:8])
Write puthon code using the in operator that determines whether ‘p’ is in s1.
Part -a) s1= "Canada"
s1[2]= 'T'
This will produce an error because strings in python are immutable i.e. you are not allowed to modify individual character of the existing string. You can just create a new string and make the changes. This is the thing which makes strings and other collections in python different from each other.
Part -b) s1 = "Hawaii is a paradise"
print(s1[2:8])
Output-
waii i
Basically, this notation s[m:n] return a sub-string of a string s starting from index m (s is a 0th indexing string) and ending at index n-1 i.e. index m is included and index n is not included.
Part -c) Write python code using the in operator that determines whether ‘p’ is in s1.
'in' operator in python is a membership operator which returns True if a sequence with the specified value is present in the object else it return False.
Code -
s1 = "Hawaii is a paradise"
print ('p' in s1)
Output -
True
What is wrong with the following code? Explain verbally. s1 = "Canada" s1[2] = ‘T’ What...
What is wrong with the following code? This piece of code incorrectly attempts to remove all even values from a stack integers. How would you fix it? You will need to write a class with the main(String[] args) {….} method to test the following piece of code, find the problem, and fix it. while (!s1.isEmpty()){ int n = s1.pop(); if (n % 2 != 0) { s1.push(n); // if odd put it back. } }
Python: 1) What output is generated by the following code segment? table = [["T", "U", "V"], ["W", "X", "Y"]] print(table[0]) Group of answer choices a- T b- ["T", "W"] c- ["T", "U", "V"] d- [["T", "U", "V"], ["W", "X", "Y"]] 2) Consider the following code segment: values = [4, 12, 0, 1, 5] print(values[1]) What is displayed when it runs? Group of answer choices a- 0 b- 1 c- 4 d- 12 3) Consider the following code segment: x =...
s1 = 'Practice How to Use String Object' Write a python statement that splits the string, creating the following list: ['Practice', 'How', 'to', 'Use', 'String', 'Object'] 2. Assume reply references a string. The following if statement determines whether reply is equal to ‘Y’ or ‘y’: reply = input("Y or y to continue: ") if reply == "Y" and reply == "y": print("Y --- <uppercase> has been selected. ") else: print("y --- <lowercase> has been chosen. ") print("… continue...
1. Briefly explain what is wrong (exactly 1 error) with the following code. auto input = 0.0; cout << "Enter input: "; cin >> input; cout << "Result 1 = " << 1.0 / input << endl; cout << "Result 2 = << input * input 2 * input << endl; cout << "Result 3 = << input + (9- input) << endl; 11
What is wrong with the following function? def print_circle_area(radius): print(3.14*radius**2) a) the function body(code) is not indented correctly b) radius should be r c) it's missing a return value d) nothing seems to be wrong with this
c++ What is wrong with the following code segment? Fix the error and explain why its wrong. int* m[3]; for(int i = 0; i < 3; i++) m[i] = new int[4]; delete [] m;
What is wrong with the following program? Explain how you will fix it in the code. #include int main() { int i; int *ptr = &i; scanf("%d", &ptr); printf("The value of i is: %d\n", *ptr); return 0; }
What is wrong the following code? Explain. public class Test { public static void main(String[] args) { A a = new A(5.0); } class A { int value = 2; } }
Describe what is wrong with the syntax of each the following blocks of code, and show how to fix it. while answer != 'q': answer = input('Word? ') print(len(answer))
Q3. (2 pts) Suppose $s0 = 1810, $s1 = 710. After running the following code, what is value held in $to and $t1 (represented in decimal form)? divu $s0, $s1 mfhi $t0 mflo $t1