Tag: programming languages

Questions Related to programming languages

What is the output of following code snippet ? void main() { printf(printf("anna")); }

  1. anna

  2. compile time error

  3. anna4

  4. non of the above

  5. garbage


Correct Option: C
  1. protected

  2. private

  3. public

  4. friendly


Correct Option: C
Explanation:

To answer this question, the user needs to know the concept of access modifiers in object-oriented programming. Access modifiers are keywords that determine the accessibility of classes, fields, methods, and other members of a class from other classes and packages. A top-level class is a class that is not a nested class or an inner class.

Now, let's go through each option and explain whether it is right or wrong:

A. protected: A protected access modifier can be applied to class members but not to top-level classes. Protected members can be accessed from within the class itself, its subclasses, and classes in the same package.

B. private: A private access modifier can be applied to class members but not to top-level classes. Private members can only be accessed from within the class itself.

C. public: A public access modifier can be applied to top-level classes, as well as class members. Public members can be accessed from anywhere in the program.

D. friendly: There is no such access modifier as "friendly". It may be a typo or a misunderstanding of the default access modifier, which is applied when no access modifier is specified. The default access modifier allows access to members from within the same package.

Therefore, the correct answer is:

The Answer is: C

  1. 4

  2. 2

  3. 8

  4. The number of bytes to represent an int is compiler dependent.


Correct Option: A
  1. arr.length() – 1

  2. arr.length()

  3. arr.length - 1

  4. arr.length


Correct Option: D
Explanation:

The correct way of getting the number of elements in a one-dimensional array is:

A. arr.length

The option A, arr.length, returns the length of the array, which represents the number of elements in the array. This is the correct way to get the number of elements in the array.

The other options are incorrect:

B. arr.length() – 1: This option subtracts 1 from the length of the array, which does not give the correct count of elements in the array.

C. arr.length(): Adding parentheses after length is not necessary and will result in a syntax error.

D. arr.length - 1: This option subtracts 1 from the length of the array, which does not give the correct count of elements in the array.

So, the correct answer is: A. arr.length