Skip to content
CodeKruPro

Four threads increment one counter

QuizMediumJava onlyAsked in: Accenture, Capgemini, Cognizant, Infosys, TCS

Four threads each run count++ 100,000 times on a shared static int count. Which statements are true? Pick all that apply.

  • The final value can be less than 400,000
  • Making the field volatile fixes it
  • Using an AtomicInteger and incrementAndGet() gives exactly 400,000
  • Doing the increment inside a synchronized block on one shared lock gives exactly 400,000

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in