amcat automata fix question and answer
In this post, you will find amcat automata mock test which will consist of automata fix questions in amcat.
Number of Questions: 7
Module Duration: 20 minutes
Detailed Syllabus:
Control Structures
conditional statements
Linear data structures
Advanced data structures
Sorting and searching algorithms
Sample Question:
1) Find
the error in the below code without modifying the logic.
Expected output:
choice is 2
Expected output:
choice is 2
#include
void main()
{
float x = 2.2;
switch (x)
{
case 2: printf("Choice is 2");
break;
default: printf("Invalid choice");
break;
}
}
void main()
{
float x = 2.2;
switch (x)
{
case 2: printf("Choice is 2");
break;
default: printf("Invalid choice");
break;
}
}
Answer:
#include
void main()
{
int x = 2; //rectifying syntax error
switch (x)
{
case 2: printf("Choice is 2");
break;
default: printf("Invalid choice");
break;
}
}
void main()
{
int x = 2; //rectifying syntax error
switch (x)
{
case 2: printf("Choice is 2");
break;
default: printf("Invalid choice");
break;
}
}
The expression used in the switch must be an integral type (int, char, and enum).
Any other type of expression is not allowed.
2) Find
the logical error in the below code.
expected output:
1 1 1 1 1 1
2 2 2 2 2
3 3 3 3
4 4 4
5 5
6
1 1 1 1 1 1
2 2 2 2 2
3 3 3 3
4 4 4
5 5
6
void
main () {
int i, j, n = 6;
for(i=1; i<=n; i++)
{
for(j=1;j<=n;j++);
{
printf("%d", i);
}
printf("\n");
}
}
int i, j, n = 6;
for(i=1; i<=n; i++)
{
for(j=1;j<=n;j++);
{
printf("%d", i);
}
printf("\n");
}
}
if we will execute the above program we will get this output:
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 56 6 6 6 6 6
Solution:
void
main () {
int i, j, n = 6;
for(i=1; i<=n; i++)
{
for(j=i;j<=n;j++)
{
printf("%d", i);
}
printf("\n");
}
}
int i, j, n = 6;
for(i=1; i<=n; i++)
{
for(j=i;j<=n;j++)
{
printf("%d", i);
}
printf("\n");
}
}
3) Complete the given program to get the output as below.
./hello
output: hello, world
./hello Ram
output: hello, Ram
./hello Ram Rahim
output: hello, Ram and Rahim
Where program name is hello and ./hello is a command to compile the program.
#include <cs50.h> #include <stdio.h>
int main(int argc, string argv[])
{
if (argc == 2)
{
printf("hello, %s\n", argv[1]);
}
else if(?)
{printf(
"hello, %s and %s\n", ?,?); }
else
{
printf("hello, world\n");
}
}
Solution:
#include <cs50.h> #include <stdio.h>
int main(int argc, string argv[])
{
if (argc == 2)
{
printf("hello, %s\n", argv[1]);
}
else if(
argc == 3)
{printf(
"hello, %s and %s\n", argv[1], argv[2]); }
else
{
printf("hello, world\n");
}
}
- Here argc means number of argument.
- for command: ./hello (argc is 1)
- for command: ./hello Ram (argc is 2)
- for command: ./hello Ram Rahim (argc is 3)
- Here argv is an argument vector.
- for command: ./hello Ram Rahim
- (argv[0] contain the compilation command)
- here, argv[0] is ./hello, argv[1] is Ram and argv[2] is Rahim.
Last question is not clear enough!
ReplyDeleteargc is an argument count and argv is an argument vector.
DeleteSahi
ReplyDeleteThere are alternative Joker123 links for players, such as Joker123 net or it can be said as Joker123 Casino which provides many Joker games. joker123 deposit pulsa
ReplyDelete