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

Predict The Output Decodex

The document contains a series of programming questions and their correct answers related to C code snippets. Each question tests knowledge of string manipulation, loops, and basic programming concepts. The correct answers are provided for each question, indicating the expected output or behavior of the code.

Uploaded by

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

Predict The Output Decodex

The document contains a series of programming questions and their correct answers related to C code snippets. Each question tests knowledge of string manipulation, loops, and basic programming concepts. The correct answers are provided for each question, indicating the expected output or behavior of the code.

Uploaded by

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

predict the output

1.Question: What will be printed by this code?


#include <stdio.h>
#include <string.h>

int main() {
char str[] = "programming";
int seen[256] = {0};

for (int i = 0; str[i] != '\0'; i++) {


if (seen[str[i]] == 0) {
printf("%c ", str[i]);
seen[str[i]]++;
}
}
return 0;
}

A) p r o g a m i n
B) p r o g a m
C) p r o g a
D) g r a m
Correct Answer: A

2.How many times will the inner loop execute for the string "Hello"?

#include <stdio.h>
int main() {
char str[] = "Hello";
for (int i = 0; str[i] != '\0'; i++) {
for (int j = 0; j < 2; j++) {
printf("%c ", str[i]);
}
}
return 0;
}

A) 5
B) 10
C) 2
D) 7
Correct Answer: B

3. What will be printed by this code?

#include <stdio.h>
int main() {
char str[] = "Hello";
int sum = 0;

for (int i = 0; str[i] != '\0'; i++) {


for (int j = i; j < i + 1; j++) {
sum += str[j];
}
}

printf("Sum of ASCII values: %d\n", sum);


return(0);
}

A) Sum of ASCII values: 500


B) Sum of ASCII values: 497
C) Sum of ASCII values: 505
D) None
Correct Answer: A

4.Predict the Output

#include <stdio.h>
int main() {
char str[] = "abcde";
char maxChar = str[0];

for (int i = 1; str[i] != '\0'; i++) {


if (str[i] > maxChar)
maxChar = str[i];
}

printf("Maximum character is : %c\n", maxChar);


return(0);
}

A) Maximum character is : a
B) Maximum character is : e
C) Maximum character is : d
D) Maximum character is : c
Correct Answer: B

5.Predict the Output

#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char *ptr;

ptr = strchr(str, 'o');


printf("First occurrence of 'o' is at: %s\n", ptr);

return 0;
}

A) First occurrence of 'o' is at: o, World!


B) First occurrence of 'o' is at: Hello
C) First occurrence of 'o' is at: o
D) First occurrence of 'o' is at: Not found
Correct Answer: A

6.Predict the Output

#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Good ";
char str2[] = "Morning";
strncpy(str1 + 5, str2, 7); // Copying part of str2 to str1
printf("%s\n", str1);

return(0);
}

A) Good Morning
B) Good Morni
C) Good Mornin
D) Good Morn
Correct Answer: B

7.What will be the output if the user inputs 300?

#include <stdio.h>
int main() {
float amount, discount;
printf("Enter purchase amount: ");
scanf("%f", &amount);
if (amount > 0) {
if (amount < 100) {
discount = 0;
} else if (amount < 500) {
discount = 0.1 * amount; // 10% discount
} else {
discount = 0.2 * amount; // 20% discount
}
printf("Discount: %.2f\n", discount);
printf("Final amount: %.2f\n", amount - discount);
} else {
printf("Invalid amount\n");
}
return 0;
}

A)
Discount: 0.00
Final amount: 50.00
B)
Discount: 5.00
Final amount: 45.00
C)
Discount: 10.00
Final amount: 40.00
D)
Invalid amount
Correct Answer: A

8.What will be the output if the user inputs the values 5 and 10 for variables a
and b respectively?
#include <stdio.h>

void swap(int *x, int *y) {


*x = *x + *y;
*y = *x - *y;
*x = *x - *y;
}
int main() {
int a = 5, b = 10;
swap(&a, &b);
printf("a: %d, b: %d\n", a, b);
return 0;
}

A)
a: 5, b: 10
B)
a: 10, b: 5
C)
a: 15, b: 5
D)
Invalid input
Correct Answer: B

9.What will be printed before sorting if the input string is "simplyeasylearning"?

#include <stdio.h>
#include <string.h>
int main(void) {
char string[] = "simplyeasylearning"; // Input string
char temp; // Temporary variable for swapping
int i, j;
int n = strlen(string); // Length of the string
printf("String before sorting: %s\n", string);

// Bubble sort algorithm to sort the characters


for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (string[i] > string[j]) {
// Swap characters if they are out of order
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}

printf("String after sorting: %s\n", string);


return 0;
}

A)
String before sorting: simplyeasylearning
B)
String before sorting: learningeasy
C)
String before sorting: aaeegiillmnnprssyy
D)
String before sorting: None
Correct Answer: A

10.

You might also like