Wednesday, November 28, 2018

Array MCQs Set-2 Answer


C Programming - MCQs based on an Array Set-2 Answer


For declaration int number[10]; suppose the starting address of number[0] is 2202 and the size of int is 4 bytes in the system; then, the address of number[1] will be ______.
A. 2206
B. 2204
C. 2203
D. none of these
ANSWER: A

For declaration char fname[15]; suppose the starting address of fname[0] is 1200. Then, the address of fname[1] will be ______.
A. 1204
B. 1202
C. 1201
D. none of these
ANSWER: C

Array can be initialized at ____________ .
A. declaration time
B. runtime
C. declaration and runtime
D. none of these
ANSWER: C

Which of the following is correct definition of one dimensional array declaration?
A. data_type array_name[arraysize];
B. array_name data_type[arraysize];
C. data_type arraysize[array_name];
D. none of these
ANSWER: A

Which of the following is correct syntax of one dimensional array declaration?
A. int array1[10];
B. array1 int[10];
C. int 10[array1];
D. none of these
ANSWER: A


Tuesday, November 20, 2018

Array MCQs Set-2


C Programming - MCQs based on an Array Set-2

 

For declaration int number[10]; suppose the starting address of number[0] is 2202 and the size of int is 4 bytes in the system; then, the address of number[1] will be ______.
A. 2206
B. 2204
C. 2203
D. none of these 

For declaration char fname[15]; suppose the starting address of fname[0] is 1200. Then, the address of fname[1] will be ______.
A. 1204
B. 1202
C. 1201
D. none of these 

Array can be initialized at ____________ .
A. declaration time
B. runtime
C. declaration and runtime
D. none of these

Which of the following is correct definition of one dimensional array declaration?
A. data_type array_name[arraysize];
B. array_name data_type[arraysize];
C. data_type arraysize[array_name];
D. none of these

Which of the following is correct syntax of one dimensional array declaration?
A. int array1[10];
B. array1 int[10];
C. int 10[array1];
D. none of these

Check your ANSWER here...


For more details visit: C Program Practicals - Question Bank 

* * *



Saturday, November 17, 2018

Moving Object on Screen


This program will move designed object (arrow sign "==>") on screen without use of <graphics.h> and system("cls") or clrscr() function.

#include <stdio.h>
#include <windows.h>

COORD c = {0, 0};
void setxy (int x, int y)
{
 c.X = x; c.Y = y; // Set X and Y coordinates
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

int main() 

  int i;
  for(i=0; i<20; i++)
  {
    system("color A1");

    setxy(i, 5);
    printf("==>");

    setxy(i-3, 5); //For clearing previous position of arrow
    printf("   ");

    Sleep(200);
  }
  return 0;  
}

Output of Program

This program will move ==> sign on screen from Left side to Right side.


For more such interesting programs visit: C Program Practicals/Games

Friday, November 9, 2018

How to change text color in C

This practical will show how to change text color in C Program.

It will Blink Colorful New Year Message on screen without <graphics.h>.

#include<stdio.h>
#include<windows.h>
int main()
{
  int i;
  char *str[] = {"color A1", "color B1","color C1", "color D1"};

  for(i=0; i<4; i++)
  {  
    system(str[i]);
    printf(".....Wishing You and Your Family.....\n\n");
    printf(".....Happy and Prosperous New Year.....");
    Sleep(500);
    system("cls");
    Sleep(500);
  }
  return 0;
}

Output of Program: Following colorful message will blink on screen.

Tuesday, November 6, 2018

String - Predict Output-2 Answer

Predict Program Output Based on C String

Output of following C Programs are not very simple. These are simple but very interesting top c programs based on C Strings asked during viva and interviews.

Program-1

#include<stdio.h>
int main()
{
    char str[] = "%d";
    str[1] = 'c';
    printf(str, 97);
    return 0;
}

Output: a
Justification:
-> Statement char str[] = "%d" will initialize str[] to "%d".
-> str[1] = 'c'; assigns 'c' to str[1] which replaces "%d" with "%c".
-> printf() function prints character of ASCII value 97; hence final output is a.
 
Program-2

#include<stdio.h>
int main()
{
    printf("C Program Practicals", "C Programming", "ICP");
    return 0;
}

Output: C Program Practicals
Justification: Comma operator has Left to Right associativity; printf() starts printing with first double quote and ends at second double quote hence final output is C Program Practicals.

Program-3

#include<stdio.h>
int main()
{
    char *names[] = { "AAA", "BBB", "CCC"};
    int i;
    char *temp;
   
  temp = names[0];
    names[0] = names[1];
    names[1] = temp;
   
  for(i=0; i<=2; i++)
        printf("%s ", names[i]);
    return 0;
}

Output: BBB AAA CCC
Justification: Array names[] will be initialized with AAA BBB CCCThen using temp variable, first and second array elements are inter-changed; resulting in final output as BBB AAA CCC.

Program-4

#include<stdio.h>
#include<string.h>
int main()
{
    char name[] = "Gujarat\0India\0";
    printf("%d", strlen(name));
    return 0;
}

Output: 7

Justification: String initialization terminates are first ' \0 '; hence name[] contains only "Gujarat"; strlen(name) function return 7 which is finally printed.

Read more interesting programs at C Program Practicals - Question Bank.

Monday, November 5, 2018

String - Predict Output-2

Predict Program Output Based on C String

Output of following C Programs are not very simple. These are simple but very interesting top c programs based on C Strings asked during viva and interviews.

Program-1

#include<stdio.h>
int main()
{
    char str[] = "%d";
    str[1] = 'c';
    printf(str, 97);
    return 0;
}

Program-2

#include<stdio.h>
int main()
{
    printf("C Program Practicals", "C Programming", "ICP");
    return 0;
}

Program-3

#include<stdio.h>
int main()
{
    char *names[] = { "AAA", "BBB", "CCC"};
    int i;
    char *temp;
   
  temp = names[0];
    names[0] = names[1];
    names[1] = temp;
   
  for(i=0; i<=2; i++)
        printf("%s ", names[i]);
    return 0;
}

Program-4

#include<stdio.h>
#include<string.h>
int main()
{
    char name[] = "Gujarat\0India\0";
    printf("%d", strlen(name));
    return 0;
}
Check your Answer here...


Read more interesting programs at C Program Practicals - Question Bank.

Sunday, November 4, 2018

C Pointer - Predict output- Answer

Predict Program Output Based on C Pointers

Output of following C Programs are not very simple. These are simple but very interesting top c programs asked during viva and interviews.


Program-1


#include<stdio.h> 
int main() 

    int x = 10, *y, *z; 
    y = &x;  
    z = y; 
    printf("%d, %d, %d", x, *y, *z); 
    return 0; 
}

Output: 10, 10, 10
Justification: Address of variable x is assigned to pointer variable y. *y means value stored at memory address stored in y which is 10. Value stored in pointer y is stored in z; hence z also prints 10, resulting in output 10, 10, 10.

Program-2

#include "stdio.h" 
int main() 

    char str[] = { 'A', 'B', 'C', 'D' }; 
    char *p = &str[1]; 
    *p++;
    printf("%c ", *p);
    *p++;
    printf("%c ", *p);


Output: C D
Justification: Address of str[1] with value 'B' is stored in pointer p; which is incremented by one and printing 'C' first and then 'D'.


Program-3

#include "stdio.h" 
int main() 

    int marks[] = { 50, 60, 70, 80 }; 
    int *p = &marks[0]; 
    *p++;
    printf("%d ", *p);
    *p = *p+2;
    printf("%d ", *p);


Output: 60 62
Justification: -Address of marks[0] is stored in pointer p; which is incremented by one, hence prints 60 first. 


*p = *p+2;

Value of *p is 60; 2 is added to value it resulting in 62. Hence final output is 60 62.

Program-4

#include <stdio.h>
int main() 

    int marks[] = { 50, 60, 70, 80 }; 
    int *p = &marks[0]; 
    *p++;
    printf("%d ", *p);
    *p++;
printf("%d ", *p);


Output: 60 70
Justification: Address of marks[0] is stored in pointer p; this memory address is incremented by one; and points to second element of an array and prints 60. Again is it incremented by one resulting final output as 60 70. 

Program-5

#include <stdio.h> 
int main() 

    int *ptr; 
    *ptr = 5; 
    printf("%d", *ptr); 
    return 0; 



Output: This will results in run time error. 
Justification: Pointer variable (*ptr) cannot be initialized.