Android NDK Interview Questions
JNI, native memory, and performance.
50 questions in this topic · 8 sample questions below
Practice NDK in the quiz engine
Sample questions
What is the primary purpose of the Android NDK?
Why: The NDK lets you compile C/C++ into native libraries loaded via JNI, typically for CPU-heavy work. It does not replace Gradle, which still orchestrates the build, and it never removes the need for ART.
In JNI, what is the fundamental difference between a JavaVM pointer and a JNIEnv pointer?
Why: There is one JavaVM per process and it may be cached and shared across threads, but each JNIEnv is thread-local and must never be reused on another thread. The reversed claim confuses which one is thread-bound.
A native thread you created with pthread_create wants to call into Java. What must it do first?
Why: A thread not created by the JVM has no JNIEnv until you AttachCurrentThread, and it should DetachCurrentThread before exiting. Reusing a JNIEnv from another thread is undefined behavior, and GetEnv returns JNI_EDETACHED for unattached threads.
You cache a jclass returned by FindClass in a static C++ variable during a JNI call and reuse it on later calls. What is the latent bug?
Why: FindClass returns a local reference, which is only valid for the duration of the current native call; to cache it you must promote it with NewGlobalRef. Caching the raw local reference leads to use of a stale, freed reference on the next call.
When are JNI local references normally freed?
Why: Local references are freed automatically when the native method returns, so short calls rarely need manual cleanup. They are not freed line-by-line, and while long-running loops can exhaust the local reference table, they do not leak forever once the frame is popped.
A native loop creates thousands of jobject local references per iteration without deleting them and eventually aborts. What is the correct fix?
Why: The local reference table has a bounded capacity (commonly 512 guaranteed slots), so unbounded creation in a loop overflows it; DeleteLocalRef frees slots as you go. Global references would make the leak worse, and largeHeap affects the Java heap, not the JNI reference table.
What does GetStringUTFChars return, and why must ReleaseStringUTFChars be called?
Why: GetStringUTFChars gives a null-terminated modified-UTF-8 buffer that the runtime may allocate as a copy, so the matching Release is required to avoid a native memory leak. It is modified-UTF-8, not standard UTF-8, which matters for embedded null bytes and supplementary characters.
How does modified UTF-8 used by GetStringUTFChars differ from standard UTF-8?
Why: Modified UTF-8 encodes U+0000 as the two-byte sequence 0xC0 0x80 so C strings never contain an interior null, and it encodes characters above the BMP as CESU-8 style surrogate pairs. Treating the buffer as standard UTF-8 can corrupt emoji and text with embedded nulls.
Practice all 50 NDK questions
These 8 are a sample. The full NDK bank is scored, tracks your progress, and explains every answer.
More Android interview topics
- Android Hilt interview questions
- Android Coroutines & Flow interview questions
- Android Room interview questions
- Android Design Patterns interview questions
- Android Mobile System Design interview questions
- Android Coding Interview Patterns interview questions
- Android Sensors interview questions
- Android Security interview questions
- Android Jetpack Compose interview questions
- Android Canvas & Animation interview questions
- Android CI/CD interview questions
- Android Git interview questions
- Android Unit Testing interview questions
- Android Kotlin interview questions
- Android Retrofit interview questions
- Android Architecture interview questions
- Android Android Framework interview questions
- Android Kotlin Multiplatform interview questions
- Android WorkManager & Background interview questions
- Android Performance & Memory interview questions