C and Java Programming Quiz

Quiz covering C and Java programming concepts including operators, memory management, exception handling, and file operations

10 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

Which of the following denotes file modes that can be used in C program?

  1. "r"
  2. "w"
  3. "a"
  4. all the above
Question 2 Multiple Choice (Multiple Answers)

Given: 1. class Super { 2. private int a; 3. protected Super(int a) { this.a = a; } 4. } ... 11. class Sub extends Super { 12. public Sub(int a) { super(a); } 13. public Sub() { this.a = 5; } 14. } Which two, independently, will allow Sub to compile? (Choose two.)

  1. Change line 2 to:public int a;
  2. Change line 2 to:protected int a;
  3. Change line 13 to:public Sub() { this(5); }
  4. Change line 13 to:public Sub() { super(5); }
Question 3 Multiple Choice (Single Answer)

What will happen? int main(int argc, char*argv) { char ptr = NULL; free(ptr); return 0; }

  1. core dump
  2. nothing
  3. undefined behavior
  4. all the above
Question 4 Multiple Choice (Multiple Answers)

Given: 31. // some code here 32. try { 33. // some code here 34. } catch (SomeException se) { 35. // some code here 36. } finally { 37. // some code here 38. } Under which three circumstances will the code on line 37 be executed? (Choose three.)

  1. The instance gets garbage collected.
  2. The code on line 33 throws an exception.
  3. . The code on line 35 throws an exception
  4. The code on line 31 throws an exception
  5. The code on line 33 executes successfully
Question 5 Multiple Choice (Single Answer)

The code used for denoting unsigned decimal integer in scanf is :

  1. %d
  2. %ud
  3. %u
  4. all the above
Question 6 Multiple Choice (Single Answer)

Which of the following differences between malloc and calloc are true? 1) malloc allocates number of bytes passed as argument 2) calloc allocates the product of number of elements multiplied by the size of each element, which are both passed as arguments. 3) both malloc and calloc return void* 4) both malloc and calloc initialize allocated memory to all 0

  1. 1, 2, 3, 4
  2. 1, 2, 3
  3. 1,2
  4. 1
Question 7 Multiple Choice (Single Answer)

In theory, which is faster, the call to strcpy or the call to memcpy? #include <string.h> #include <stdlib.h> int main(){ char msg[12] = "Hello World"; char buffer1[12]; char buffer2[12]; strcpy(buffer1, msg); memcpy(buffer2, msg, sizeof(msg)); return 0; }

  1. strcpy
  2. memcpy
  3. both memcpy and strcpy
  4. none
Question 8 Multiple Choice (Single Answer)

What is the output?int i = 3;if (!i) i++; i++;if (i==3) i+=2; i+=2;printf("%d\n", i);

  1. 3
  2. 5
  3. 7
  4. 6
Question 9 Multiple Choice (Single Answer)

Which takes the first precedence in evaluation among the following operators?

  1. ||
  2. !
  3. &&
  4. All the Above has equal precedence
Question 10 Multiple Choice (Single Answer)

What gets printed? int i = 3; if (!i) i++; i++; if (i==3) i+=2; i+=2; printf("%d\n", i);

  1. 6
  2. 7
  3. 3
  4. 5