Prepare and test your skills

Prepare and test your skills

Free sample question with answers and detailed explanation below. The Professional Cloud Developer practice bank has an additional 608 practice questions.Try 10 free realistic questions: instant score, no credit card, and no account required.
You are developing a C# (.NET) application on Linux that publishes and subscribes to messages using the Cloud Pub/Sub client library. To run local tests without affecting production resources, you perform the following steps:
gcloud beta emulators pubsub start$(gcloud beta emulators pubsub env-init) in your terminal session to configure PUBSUB_EMULATOR_HOSTDespite these steps, your application still attempts to connect to the production Google Cloud Pub/Sub endpoint and fails with an authentication error.
What should you do to redirect your application's Pub/Sub requests to the local emulator?
gcloud auth application-default login to initialize local Application Default Credentials, and export the GOOGLE_APPLICATION_CREDENTIALS environment variable before starting the application.GOOGLE_APPLICATION_CREDENTIALS doRunning gcloud auth application-default login stores your personal Google Cloud user credentials locally so that Google Cloud client libraries can automatically discover them via Application Default Credentials (ADC). Setting the GOOGLE_APPLICATION_CREDENTIALS environment variable similarly points client libraries to a local service account key JSON file for authentication.
GOOGLE_APPLICATION_CREDENTIALS only resolves the authentication error by supplying valid credentials to the production Google Cloud Pub/Sub endpoint. It does not redirect traffic to the local emulator.PUBSUB_EMULATOR_HOST.GOOGLE_APPLICATION_CREDENTIALS pose security risks if leaked or mismanaged, and should be avoided when local emulators can be used instead.EmulatorDetection is configured.Your goal were to run your local application or container directly against live Google Cloud services (for example, when no emulator exists for a service or during cloud-connected integration testing) rather than isolating tests to a local emulator.
PublisherClientBuilder and SubscriberClientBuilder, to set the EmulatorDetection property to EmulatorDetection.EmulatorOrProduction while keeping PUBSUB_EMULATOR_HOST set.EmulatorDetection configuration isThe EmulatorDetection property in the Google Cloud .NET (C#) client libraries controls whether a service client connects to a local emulator, the production Google Cloud endpoint, or checks environment variables to decide dynamically. Unlike client libraries for languages such as Python, Node.js, or Go—which automatically inspect PUBSUB_EMULATOR_HOST at runtime—the C#, Java, and Rust Pub/Sub client libraries require explicit code configuration to recognize emulator environment variables.
PUBSUB_EMULATOR_HOST: Setting EmulatorDetection = EmulatorDetection.EmulatorOrProduction on builders like PublisherClientBuilder, SubscriberClientBuilder, PublisherServiceApiClientBuilder, and SubscriberServiceApiClientBuilder instructs the library to check if PUBSUB_EMULATOR_HOST is populated.PUBSUB_EMULATOR_HOST is set by $(gcloud beta emulators pubsub env-init), the client routes gRPC requests directly to the local emulator host and port over an unauthenticated, plaintext channel.PUBSUB_EMULATOR_HOST is unset (such as in a staging or production deployment), the exact same codebase seamlessly falls back to the production Google Cloud Pub/Sub endpoint using standard authentication.EmulatorOrProduction avoids hardcoded localhost endpoints or conditional #if DEBUG compiler directives.PublisherClientBuilder, SubscriberClientBuilder) and low-level API (PublisherServiceApiClientBuilder, SubscriberServiceApiClientBuilder) clients.Because the PUBSUB_EMULATOR_HOST variable is already configured via env-init, adding EmulatorDetection.EmulatorOrProduction is the cleanest, officially supported mechanism to bridge the environment variable and the .NET client library without compromising production behavior.
gcloud CLI configuration, set auth/disable_credentials to true, and configure api_endpoint_overrides/pubsub to point to the local emulator's host and port.api_endpoint_overrides configuration doesThe gcloud config set api_endpoint_overrides/<service> property instructs the Google Cloud CLI (gcloud) itself to redirect its own command-line API requests to a custom URL instead of the default Google Cloud API endpoint. Pairing this with auth/disable_credentials true in a separate gcloud configuration allows CLI commands to communicate with certain local emulators (such as the Cloud Spanner emulator's REST endpoint) without requiring authentication tokens.
gcloud CLI configuration profiles or api_endpoint_overrides properties. Your C# application will continue targeting the production endpoint and failing authentication.gcloud pubsub CLI commands or Google Cloud console interactions; it is designed strictly for applications built with the Cloud Client Libraries.gcloud configurations affect only commands invoked through the gcloud executable, not independent application processes.PUBSUB_EMULATOR_HOST, whereas api_endpoint_overrides typically targets HTTP/REST URLs for the CLI.EmulatorDetection: Even if an override were set, the .NET client library still defaults to EmulatorDetection.ProductionOnly unless explicitly changed in code.You were configuring the gcloud CLI to manage instances and databases on a local Spanner emulator (using api_endpoint_overrides/spanner and auth/disable_credentials true), rather than redirecting a C# application's Pub/Sub client library.
A common misconception is that gcloud configurations act as global system proxies for all local Google Cloud SDKs. In reality, application client libraries rely on service-specific environment variables (and in C#, Java, and Rust, explicit builder settings) to discover emulators.
--host-port=127.0.0.1:8085 flag, and manually export both PUBSUB_EMULATOR_HOST and PUBSUB_PROJECT_ID in your terminal.--host-port and manual exports doPassing --host-port=127.0.0.1:8085 to gcloud beta emulators pubsub start binds the Pub/Sub emulator to the IPv4 loopback address (127.0.0.1) on port 8085 instead of its default IPv6 loopback address (::1). Manually exporting PUBSUB_EMULATOR_HOST and PUBSUB_PROJECT_ID sets the target host/port and optional project identifier in the shell environment, which is typically necessary when the application runs on a different machine or container than the emulator.
PUBSUB_EMULATOR_HOST by default: Even when PUBSUB_EMULATOR_HOST is exported manually with an IPv4 address, the C# Pub/Sub client library defaults to connecting only to production unless EmulatorDetection is explicitly enabled in code.env-init: Running $(gcloud beta emulators pubsub env-init) on the same machine already exports the exact PUBSUB_EMULATOR_HOST value matching the emulator's active address and dynamically assigned port.PUBSUB_EMULATOR_HOST in its default configuration, it still routes requests to pubsub.googleapis.com and triggers the authentication failure.--host-port only solves network reachability issues when IPv6 is disabled on the host OS or when binding to 0.0.0.0 for container access.PUBSUB_PROJECT_ID is optional: Setting PUBSUB_PROJECT_ID is helpful for sample scripts or tracking a local project name, but it does not control endpoint routing.8085 manually can cause mismatches if the emulator dynamically selects another port on restart without --host-port.Your application were written in Python, Node.js, or Go (which automatically read PUBSUB_EMULATOR_HOST) and was failing to connect to [::1] because IPv6 was disabled on the Linux machine.
Answer them yourself and get your score the moment you finish. No account needed.
Full access includes
Keep the momentum going with these hand-picked practice scenarios
Last updated Validated