ProgrammingVery hardProduct company interview · GATE level
What is printed on a typical platform (16-bit short, 32-bit int)?
What is printed on a typical platform (16-bit short, 32-bit int)?
int i = -1;
unsigned int u = 1;
short s = -1;
unsigned short us = 1;
cout << (i < u) << (s < us) << (i < (int)u)
<< (sizeof(int) * -1 > 0);- A. 0101
- B. 1111
- C. 0011
- D. 0111
Correct answer: D. 0111
i < u: in a comparison of int with unsigned int, the int is converted to unsigned, so -1 becomes 4294967295 and the result is false (0). s < us: both short types are first promoted to int (int can hold every unsigned short value), so -1 < 1 is true (1). i < (int)u compares two ints: true. sizeof yields an unsigned size_t, so sizeof(int) * -1 converts -1 to a huge unsigned value and the product is positive: true.
More OOP with C++ and Java questions
- Which lines fail to compile? cpp struct A { explicit A(int) { } }; struct B { B(int) { } }; void takeA(A) { } void takeB(B) { } int main()…
- What is printed? cpp struct N { int v = 0; N& operator++() { cout << "pre "; ++v; return this; } N operator++(int) { cout << "post "; N t…
- What is printed? java String s = "a,b,,c,,"; System.out.print(s.split(",").length);
- What is printed? java System.out.print(",x,".split(",").length + " " + "".split(",").length);
- What is printed? java String a = "Aa", b = "BB"; Set<String> set = new HashSet<>(); set.add(a); set.add(b); System.out.print((a.hashCode()…
- What is printed? java PriorityQueue<String> pq = new PriorityQueue<>( Comparator.comparing(String::length).thenComparing(Comparator.reverse…