0% found this document useful (0 votes)
4 views6 pages

C&PP 2marks

The document contains a series of programming questions and answers related to Advanced C and Python Programming. It covers topics such as command line arguments, error handling functions, file operations, string manipulation, and variable scope. Additionally, it includes code examples and explanations for various programming concepts in both C and Python.

Uploaded by

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

C&PP 2marks

The document contains a series of programming questions and answers related to Advanced C and Python Programming. It covers topics such as command line arguments, error handling functions, file operations, string manipulation, and variable scope. Additionally, it includes code examples and explanations for various programming concepts in both C and Python.

Uploaded by

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

23ES207 - Advanced C and Python Programming: 2 MARKS WITH ANSWER

1. What will be the values for argc and argv[] when the input “run with my values” is
passed as command line arguments?

 argc = 4

 argv values:

o argv[0] = "run"

o argv[1] = "with"

o argv[2] = "my"

o argv[3] = "values"

2. Write about different error handling functions on files.

 ferror(FILE *fp) – Detects an error with the file.

 feof(FILE *fp) – Detects end of file.

 perror("msg") – Displays system error message.

 clearerr(FILE *fp) – Resets error and EOF indicators.

3. Describe the prototype of the function fopen().

FILE *fopen(const char *filename, const char *mode);

 Opens a file with the specified mode like "r", "w", "a", etc.

4. Write a C program to get name and marks of n number of students from user and store
them in a file.

#include <stdio.h>

int main() {

FILE *f = fopen("students.txt", "w");

int n; char name[50]; float marks;

printf("Enter number of students: ");

scanf("%d", &n);
for (int i = 0; i < n; i++) {

printf("Enter name and marks: ");

scanf("%s %f", name, &marks);

fprintf(f, "%s %.2f\n", name, marks);

fclose(f);

return 0;

Execution:

Enter number of students: 2

Enter name and marks: Ravi 88.5

Enter name and marks: Kavi 92.0

Output (in students.txt):

Ravi 88.50

Kavi 92.00

5. Outline the logic to swap the contents of two identifiers without using a third variable.

int a = 5, b = 10;

a = a + b;

b = a - b;

a = a - b;

Output:

a = 10, b = 5

6. Analyze different ways to manipulate strings in Python.

s = "hello"

print(s.upper()) # HELLO

print(s[1:4]) # ell
print(s * 2) # hellohello

print(s.replace("l", "x")) # hexxo

7. Point out the rules to be followed for naming any identifier.

 Start with a letter or underscore.

 Can contain digits, letters, underscores.

 Cannot use keywords.

 Case-sensitive.

8. State about logical operators available in Python language with example.

a = True

b = False

print(a and b) # False

print(a or b) # True

print(not a) # False

9. Give the various data types in Python.

 int, float, complex

 bool, str

 list, tuple, set, dict

10. Name any two functions used in Random Access files and specify their use in C
Programming.

 fseek(): Moves the file pointer.

 ftell(): Returns current pointer position.

11. Write short notes on i. fgets() ii. fputs()

char str[50];
FILE *f = fopen("sample.txt", "r");

fgets(str, 50, f); // Reads a line

fclose(f);

f = fopen("output.txt", "w");

fputs(str, f); // Writes a line

fclose(f);

12. What will be the impact if fclose() function is avoided in a file handling C program?

 Data might not be written (buffered).

 File stays open, causing memory leaks.

 May corrupt file or lock access.

13. Write a C Program to Copy one file content into another file.

#include <stdio.h>

int main() {

FILE *src = fopen("source.txt", "r");

FILE *dest = fopen("copy.txt", "w");

char ch;

while ((ch = fgetc(src)) != EOF)

fputc(ch, dest);

fclose(src);

fclose(dest);

return 0;

Input (source.txt):

Advanced C Programming

Output (copy.txt):
Advanced C Programming

14. Write a program to iterate a range using continue statement.

for i in range(5):

if i == 2:

continue

print(i)

Output:

15. Justify the effects of slicing operations on an array.

arr = [10, 20, 30, 40, 50]

print(arr[1:4]) # [20, 30, 40]

 Slicing extracts parts without modifying original list.

16. Present the flow of execution for a while statement.

i=1

while i <= 3:

print(i)

i += 1

Output:

3
17. Comment with an example on the use of local and global variable with the same
identifier name.

x = 100 # Global variable

def func():

x = 50 # Local variable

print("Local x:", x)

func()

print("Global x:", x)

Output:

Local x: 50

Global x: 100

18. Describe various methods used on a string. (Any Four)

s = "Welcome"

print(s.upper()) # WELCOME

print(s.lower()) # welcome

print(s.find("co")) # 3

print(s.replace("e", "*")) # W*lcom*

You might also like